Reputation: 501
I've got this code to run and fetch images from my drive. But i'm running into a problem every time I run this code.
function listF() {
$result = array();
$tok = array();
$nextPageToken = NULL;
do {
try {
$parameters = array();
if ($nextPageToken) {
$parameters['pageToken'] = $nextPageToken;
$parameters['q'] = "mimeType='image/jpeg' or mimeType='image/png'";
}
$files = $this->service->files->listFiles($parameters);
$tok[] = $nextPageToken;
$result = array_merge($tok, $result, $files->getFiles());
$nextPageToken = $files->getNextPageToken();
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
$nextPageToken = NULL;
}
} while ($nextPageToken);
return $result;
}
I'm getting this error:
An error occurred: {
"error": {
"errors": [
{
"domain": "global",
"reason": "invalid",
"message": "Invalid Value",
"locationType": "parameter",
"location": "pageToken"
}
],
"code": 400,
"message": "Invalid Value"
}
}
It doesn't really seem illegitimate to me. Perhaps you might able to find the bug. Thanks
Upvotes: 0
Views: 2046
Reputation: 12644
A V3 tested solution. This will handle large sets (greater then 1000) by handling pagination:
function GetFiles()
{
$options =
[
'pageSize' => 1000,
'supportsAllDrives' => true,
'fields' => "files(id, mimeType, name), nextPageToken"
];
$files = [];
$pageToken = null;
do
{
try
{
if ($pageToken !== null)
{
$options['pageToken'] = $pageToken;
}
$response = $this->service->files->listFiles($options);
$files = array_merge($files, $response->files);
$pageToken = $response->getNextPageToken();
}
catch (Exception $exception)
{
$message = $exception->getMessage();
echo "Error: $message\r\n";
$pageToken = null;
}
} while ($pageToken !== null);
return $files;
}
Upvotes: 2
Reputation: 486
The Google Drive V3 PHP API is not as copiously documented as V2.
I found no simple PHP examples utilizing pageToken
for the V3 API, so I am providing this one:
$parameters = array();
$parameters['q'] = "mimeType='image/jpeg' or mimeType='image/png'";
$parameters['fields'] = "files(id,name), nextPageToken";
$parameters['pageSize'] = 100;
$files = $google_drive_service->files->listFiles($parameters);
/* initially, we're not passing a pageToken, but we need a placeholder value */
$pageToken = 'go';
while ($pageToken != null) {
if (count($files->getFiles()) == 0) {
echo "No files found.\n";
}
else {
foreach ($files->getFiles() as $file) {
echo "name: '".$file->getName()."' ID: '".$file->getId()."'\n";
}
}
/* important step number one - get the next page token (if any) */
$pageToken = $files->getNextPageToken();
/* important step number two - append the next page token to your query */
$parameters['pageToken'] = $pageToken;
$files = $google_drive_service->files->listFiles($parameters);
}
Upvotes: 1
Reputation: 4563
It appears that the nextPageToken will be ruled invalid unless you include the exact same query field (q) in the subsequent requests that was included in the initial request.
var files = []
var nextToken;
gapi.client.drive.files.list({
'q': "mimeType='image/jpeg' or mimeType='image/png'",
'pageSize': 10,
'fields': 'nextPageToken, files(id, name)'
}).then(function(response) {
nextToken = response.result.nextPageToken;
files.push(...response.result.files)
while (nextToken) {
gapi.client.drive.files.list({
'nextPage': nextToken,
'q': "mimeType='image/jpeg' or mimeType='image/png'",
'pageSize': 10,
'fields': 'nextPageToken, files(id, name)'
}).then(function(response) {
nextToken = response.result.nextPageToken;
files.push(...response.result.files)
})
}
});
Upvotes: 2
Reputation: 17623
I'll answer your nextPageToken
problem using Javascript, just take note of the logic.
I have two listFile() functions which are identical. One executes at initial load, after loading the page, it shows the first 10 of my 100 files. The other executes each time a button is clicked.
First function to display the inital 10 files.
//take note of this variable
var nextToken ;
function listFiles() {
gapi.client.drive.files.list({
'pageSize': 10,
'fields': "*"
}).then(function(response) {
//assign the nextPageToken to a variable to be used later
nextToken = response.result.nextPageToken;
// do whatever you like here, like display first 10 files in web page
// . . .
});
}
Second function: This function is triggered by click of a button named "Next Page" which displays the succeeding files from 11 to N.
function gotoNextPage(event) {
gapi.client.drive.files.list({
'pageSize': 10,
'fields': "*",
'pageToken': nextToken
}).then(function(response) {
//assign new nextPageToken to make sure new files are displayed
nextToken = response.result.nextPageToken;
//display batch of file results in the web page
//. . .
});
}
Upvotes: 2