简体   繁体   English

如何检索特定文件夹中的所有文件?

[英]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" ); $optParams = array('pageSize' => 10, 'fields' => "nextPageToken, files(contentHints/thumbnail,fileExtension,iconLink,id,name,size,thumbnailLink,webContentLink,webViewLink,mimeType,parents)", 'q ' => "'".$folderId."' 在父母中" ); $results = $service->files->listFiles($optParams); $results = $service->files->listFiles($optParams);

Here is my code:这是我的代码:

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

$credentialsPath = "credentials.json"; $credentialsPath = "credentials.json";

if (file_exists("credentials.json")) { $access_token = (file_get_contents("credentials.json")); if (file_exists("credentials.json")) { $access_token = (file_get_contents("credentials.json")); $client->setAccessToken($access_token); $client->setAccessToken($access_token); //Refresh the token if it's expired. //如果令牌过期,则刷新令牌。 if ($client->isAccessTokenExpired()) { echo "accesstoken is expired if ($client->isAccessTokenExpired()) { echo "accesstoken 已过期
"; $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); file_put_contents($credentialsPath, json_encode($client->getAccessToken())); echo "saved getAccessToken "; $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); file_put_contents($credentialsPath, json_encode($client->getAccessToken())); echo "保存的getAccessToken
"; } $drive_service = new Google_Service_Drive($client); //$mp3array = $drive_service->files->listFiles(array())->getFiles(); works, but not listing by folder $folderid = "xxxxxxxxx"; "; } $drive_service = new Google_Service_Drive($client); //$mp3array = $drive_service->files->listFiles(array())->getFiles(); 有效,但不按文件夹列出 $folderid = "xxxxxxxxx" ;
$optParams = array( 'pageSize' => 10, 'fields' => "id, name", 'q' => "'".$folderId."' in parents" ); $optParams = array('pageSize' => 10, 'fields' => "id, name", 'q' => "'".$folderId."' in parents"); $mp3array = $drive_service->files->listFiles($optParams); $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: ."致命错误:未捕获的 Google_Service_Exception:{“error”:{“errors”:[ {“domain”:“global”、“reason”:“notFound”、“message”:“找不到文件:.”、“locationType”: “参数”,“位置”:“fileId”}],“代码”:404,“消息”:“找不到文件:。” } } 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 } } 在 /home/wosbccom/public_html/google-api-php-client-2.4.1/src/Google/Http/REST.php:119 堆栈跟踪:#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(对象(在 /home/wosbccom/public_html/google -api-php-client-2.4.1/src/Google/Http/REST.php 在第 119 行

Can you tell me what I did wrong?你能告诉我我做错了什么吗?

In your script, $folderid = "xxxxxxxxx";在您的脚本中, $folderid = "xxxxxxxxx"; is declared as $folderid.被声明为 $folderid。 But $folderId is used at 'q' => "'".$folderId."' in parents".但是 $folderId 用于 'q' => "'".$folderId."' in parents"。 I think that this spelling mistake between i and I is the reason of your current error message.我认为 i 和 I 之间的拼写错误是您当前错误消息的原因。 Can you test your script by modifying this?你可以通过修改这个来测试你的脚本吗?

In your script, $folderid = "xxxxxxxxx";在您的脚本中, $folderid = "xxxxxxxxx"; is declared as $folderid .被声明为$folderid But $folderId is used at 'q' => "'".$folderId."' in parents" .但是$folderId用于'q' => "'".$folderId."' in parents" I think that this spelling mistake between i and I is the reason of your current error message.我认为iI之间的拼写错误是您当前错误消息的原因。

Please modify as follows.请进行如下修改。

From:从:

$folderid = "xxxxxxxxx";

To:至:

$folderId = "xxxxxxxxx";

or或者

From:从:

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

To:至:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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