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.
What am I doing wrong here?
I believe your goal is as follows.
pageToken
.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
.
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)
}
nextPageToken
.Is there anyway to use 'size' as a search parameter with DriveApp.searchFiles in Google Apps Script?
The sample script of Google Apps Script using your parameters is as follows. When you use this script, please enable Drive API at Advanced Google Services.
function myFunction() {
let fileList = [];
let pageToken = "";
do {
const obj = Drive.Files.list({
"includeItemsFromAllDrives": true,
"orderBy": "title",
"maxResults": 1000,
"q": "'FOLDER_ID' in parents",
"supportsAllDrives": true,
"fields": "nextPageToken,items(title,fileExtension,fileSize,labels(starred))",
"pageToken": pageToken
});
if (obj.items.length > 0) fileList = [...fileList, ...obj.items];
pageToken = obj.nextPageToken;
} while(pageToken);
console.log(fileList)
}