简体   繁体   中英

How can I retrieve all files in a specific folder?

I found this as a solution, but it didn't work for me:

$optParams = array( 'pageSize' => 10, 'fields' => "nextPageToken, files(contentHints/thumbnail,fileExtension,iconLink,id,name,size,thumbnailLink,webContentLink,webViewLink,mimeType,parents)", 'q' => "'".$folderId."' in parents" ); $results = $service->files->listFiles($optParams);

Here is my code:

$client->setClientSecret('xxxxxx'); $client->setRedirectUri(' https://wosbc.com/oauth2callback.php '); $client->setScopes(' https://www.googleapis.com/auth/readonly '); $client->setAccessType('offline'); $client->setPrompt('consent');

$credentialsPath = "credentials.json";

if (file_exists("credentials.json")) { $access_token = (file_get_contents("credentials.json")); $client->setAccessToken($access_token); //Refresh the token if it's expired. if ($client->isAccessTokenExpired()) { echo "accesstoken is expired
"; $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); file_put_contents($credentialsPath, json_encode($client->getAccessToken())); echo "saved getAccessToken
"; } $drive_service = new Google_Service_Drive($client); //$mp3array = $drive_service->files->listFiles(array())->getFiles(); works, but not listing by folder $folderid = "xxxxxxxxx";
$optParams = array( 'pageSize' => 10, 'fields' => "id, name", 'q' => "'".$folderId."' in parents" ); $mp3array = $drive_service->files->listFiles($optParams);

Sorry for the way the code formatted. This produced the following error:

Fatal error: Uncaught Google_Service_Exception: { "error": { "errors": [ { "domain": "global", "reason": "notFound", "message": "File not found: .", "locationType": "parameter", "location": "fileId" } ], "code": 404, "message": "File not found: ." } } in /home/wosbccom/public_html/google-api-php-client-2.4.1/src/Google/Http/REST.php:119 Stack trace: #0 /home/wosbccom/public_html/google-api-php-client-2.4.1/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 /home/wosbccom/public_html/google-api-php-client-2.4.1/src/Google/Task/Runner.php(176): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 /home/wosbccom/public_html/google-api-php-client-2.4.1/src/Google/Http/REST.php(58): Google_Task_Runner->run() #3 /home/wosbccom/public_html/google-api-php-client-2.4.1/src/Google/Client.php(842): Google_Http_REST::execute(Object( in /home/wosbccom/public_html/google-api-php-client-2.4.1/src/Google/Http/REST.php on line 119

Can you tell me what I did wrong?

In your script, $folderid = "xxxxxxxxx"; is declared as $folderid. But $folderId is used at 'q' => "'".$folderId."' in parents". I think that this spelling mistake between i and I is the reason of your current error message. Can you test your script by modifying this?

In your script, $folderid = "xxxxxxxxx"; is declared as $folderid . But $folderId is used at 'q' => "'".$folderId."' in parents" . I think that this spelling mistake between i and I is the reason of your current error message.

Please modify as follows.

From:

$folderid = "xxxxxxxxx";

To:

$folderId = "xxxxxxxxx";

or

From:

'q' => "'".$folderId."' in parents

To:

'q' => "'".$folderid."' in parents

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM