Search code examples
google-apps-scriptgoogle-drive-api

How to get a pageToken for the files.list API endpoint of the Google Drives API?


I'm using the Google Drives API for the first time and I cannot find anywhere how to use the API's pageToken system. Below is a snippet of example code taking directly from googles web editor.

<script src="https://apis.google.com/js/api.js"></script>
<script>
  /**
   * Sample JavaScript code for drive.files.list
   * See instructions for running APIs Explorer code samples locally:
   * https://developers.google.com/explorer-help/guides/code_samples#javascript
   */
  function execute() {
    return gapi.client.drive.files.list({
      "includeItemsFromAllDrives": true,
      "orderBy": "name",
      "pageSize": 1000,
      "q": "'FOLDER_ID' in parents",
      "supportsAllDrives": true,
      "fields": "files(name,fileExtension,starred,size)"
    })
        .then(function(response) {
                // Handle the results here (response.result has the parsed body).
                console.log("Response", response);
              },
              function(err) { console.error("Execute error", err); });
  }
  gapi.load("client:auth2", function() {
    gapi.auth2.init({client_id: "YOUR_CLIENT_ID"});
  });
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>

Currently, the code above, only returns 1000 files. According to the documentation it should also include a nextPageToken, but the example code does not return that token. enter image description here

What am I doing wrong here?


Solution

  • I believe your goal is as follows.

    • You want to retrieve the file list using the method of "Files: list " in Drive API v3 with googleapis for Javascript.
      • In this case, you want to use pageToken.
    • You have already been able to get values from Google Drive using Drive API.

    In this case, how about the following modification? When you use the property of fields, in order to return the value of nextPageToken, it is required to include nextPageToken in the value of fields.

    Modified script:

    function getList(pageToken) {
      return new Promise((resolve, reject) => {
        gapi.client.drive.files.list({
          "includeItemsFromAllDrives": true,
          "orderBy": "name",
          "pageSize": 1000,
          "q": "'FOLDER_ID' in parents",
          "supportsAllDrives": true,
          "fields": "files(name,fileExtension,starred,size),nextPageToken",
          "pageToken": pageToken
        }).then(res => resolve(res)).catch(err => reject(err));
      });
    }
    
    async function execute() {
      let fileList = [];
      let pageToken = "";
      do {
        const obj = await getList(pageToken).catch(err => console.log(err));
        if (obj.result.files.length > 0) fileList = [...fileList, ...obj.result.files];
        pageToken = obj.result.nextPageToken;
      } while (pageToken);
      console.log(fileList)
    }
    
    • When this script is run, the file list over 1000 files is retrieved using nextPageToken.

    Note:

    Reference: