下载文件时出现403错误

时间:2020-03-05 20:18:50

标签: download google-drive-api

尝试从驱动器下载任何文件时出现403错误。 以下功能用于下载:

        public function downloadFile($fileData)
        {
            $this->googleClient = $this->getClient(true);
            $fileId = $fileData['id'];
            $fileSize = (int)$fileData['size'];

            $http = $this->googleClient->authorize();
            $chunkSizeBytes = 1 * 1024 * 1024;
            $chunkStart = 0;
            $tmpFile = tmpfile();
            $metaData = stream_get_meta_data($tmpFile);
            fclose($tmpFile);

            $newFile = fopen($metaData["uri"], "wb");
            while ($chunkStart < $fileSize)
            {
                $chunkEnd = $chunkStart + $chunkSizeBytes;
                $response = $http->request(
                    'GET',
                    sprintf('/drive/v3/files/%s', $fileId),
                    [
                        'query' => ['alt' => 'media'],
                        'headers' => [
                            'Range' => sprintf('bytes=%s-%s', $chunkStart, $chunkEnd)
                        ]
                    ]
                );
                $chunkStart = $chunkEnd + 1;
                fwrite($newFile, $response->getBody()->getContents());
            }
            fclose($newFile);
            return $metaData['uri'];
        }

客户端设置:

            $client = new Google_Client();
            $client->setApplicationName('Google Drive load files');
            $client->setScopes(Google_Service_Drive::DRIVE);

            $client->setAuthConfig($this->configuration['gdriveCredentialsJson']);
            $client->setAccessType('offline');
            $client->setAccessToken($this->configuration['gdriveTokensJson']);

在开始下载文件之前,我正在生成新的刷新令牌。

返回以下错误:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "appNotAuthorizedToFile",
    "message": "The user has not granted the app 436111396855 read access to the file 1hUxQkXVFsOCoRnxANDVqlf8pIWV5Qssk.",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 403,
  "message": "The user has not granted the app 436111396855 read access to the file 1hUxQkXVFsOCoRnxANDVqlf8pIWV5Qssk."
 }
}

我在“管理应用访问控制”中将我的应用添加到了第三方应用中,并使其受到信任 我还在“管理API客户端访问”中配置了应用范围 我想念的是什么?

0 个答案:

没有答案