Google TeamDrive文件->获取命令,找不到文件

时间:2018-11-08 16:25:58

标签: php download google-api

我能够很好地搜索teamdrive,但目前无法使用file get命令使用服务帐户下载搜索到的文件。

$client = new Google_Client();
$client->setApplicationName('Google Drive API PHP Quickstart');
$client->setScopes(Google_Service_Drive::DRIVE);
$client->setAuthConfig(__DIR__ . '/api-access-221800-67b8a6c7d16d.json');
//$client->setAccessType('offline');
$service = new Google_Service_Drive($client);

// Print the names and IDs for up to 10 files.
$optParams = array(
  'pageSize' => 20,
  'corpora' => "user,allTeamDrives",
  'includeTeamDriveItems' => true,
  'q' => "name contains '606399' and mimeType != 'application/vnd.google-apps.folder'",
  'supportsTeamDrives' => true
);
$results = $service->files->listFiles($optParams);
echo '<pre>';
print_r($results);
echo '</pre>';
if (count($results->getFiles()) == 0) {
    print "No files found.\n";
} else {
    print "Files:\n";
    foreach ($results->getFiles() as $file) {
        printf("%s (%s)\n", $file->getName(), $file->getId());
    }
}

$response = $service->files->get(array(
    'fileId' => "1bzEsw6Wrj_aNQPZOS7jlpK6mBYXS9SWU",
    'alt' => 'json',
    'acknowledgeAbuse' => true,
    'supportsTeamDrives' => true
  ));

$content = $response->getBody()->getContents();

此应用程序的输出如下。

Google_Service_Drive_FileList Object
(
    [collection_key:protected] => files
    [filesType:protected] => Google_Service_Drive_DriveFile
    [filesDataType:protected] => array
    [incompleteSearch] => 
    [kind] => drive#fileList
    [nextPageToken] => 
    [internal_gapi_mappings:protected] => Array
        (
        )

    [modelData:protected] => Array
        (
            [files] => Array
                (
                    [0] => Array
                        (
                            [kind] => drive#file
                            [id] => 1hoaB13IcMZWKSlCuKIHtfYcr3-O4xO-2
                            [name] => 606399 USDA.pdf
                            [mimeType] => application/pdf
                            [teamDriveId] => zzzRemoved
                        )

                    [1] => Array
                        (
                            [kind] => drive#file
                            [id] => 1et_2Kau97OCkhk8-t9Ctx-pnZlp1ha8d
                            [name] => 606399 Kernel Pic.JPG
                            [mimeType] => image/jpeg
                            [teamDriveId] => zzzRemoved
                        )

                    [2] => Array
                        (
                            [kind] => drive#file
                            [id] => 1bzEsw6Wrj_aNQPZOS7jlpK6mBYXS9SWU
                            [name] => 606399 Load Docs.pdf
                            [mimeType] => application/pdf
                            [teamDriveId] => zzzRemoved
                        )

                )

        )

    [processed:protected] => Array
        (
        )

)
Files: 606399 USDA.pdf (1hoaB13IcMZWKSlCuKIHtfYcr3-O4xO-2) 606399 Kernel Pic.JPG (1et_2Kau97OCkhk8-t9Ctx-pnZlp1ha8d) 606399 Load Docs.pdf (1bzEsw6Wrj_aNQPZOS7jlpK6mBYXS9SWU) 
Fatal error: Uncaught Google_Service_Exception: { "error": { "errors": [ { "domain": "global", "reason": "notFound", "message": "File not found: fileId,1bzEsw6Wrj_aNQPZOS7jlpK6mBYXS9SWU,alt,json,acknowledgeAbuse,1,supportsTeamDrives,1.", "locationType": "parameter", "location": "fileId" } ], "code": 404, "message": "File not found: fileId,1bzEsw6Wrj_aNQPZOS7jlpK6mBYXS9SWU,alt,json,acknowledgeAbuse,1,supportsTeamDrives,1." } } in C:\xampp\htdocs\gapi\vendor\google\apiclient\src\Google\Http\REST.php:118 Stack trace: #0 C:\xampp\htdocs\gapi\vendor\google\apiclient\src\Google\Http\REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 C:\xampp\htdocs\gapi\vendor\google\apiclient\src\Google\Task\Runner.php(181): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 C:\xampp\htdocs\gapi\vendor\google\apiclient\src\Google\Http\REST.php(58): Google_ in C:\xampp\htdocs\gapi\vendor\google\apiclient\src\Google\Http\REST.php on line 118

我能够使用Google Drive Rest API测试器在这里https://developers.google.com/drive/api/v2/reference/files/get

处获得具有相同参数的json响应

最终目标是能够下载用户单击的文件。

1 个答案:

答案 0 :(得分:1)

您需要传递用于请求文件的可选参数作为第二个参数,files->get()的方法签名为get($fileId, $optParams = array())。因此,如果将代码更改为以下代码,则它应该可以工作:

$response = $service->files->get("1bzEsw6Wrj_aNQPZOS7jlpK6mBYXS9SWU", array(
    'alt' => 'json',
    'acknowledgeAbuse' => true,
    'supportsTeamDrives' => true
  ));

可以找到方法定义here