首页 文章

如何使用Google Drive API(v3)阅读共享工作表的内容? authError

提问于
浏览
0

我想阅读用户与我为我的应用程序创建的特定用户(myapp)共享的Google文档和Google表格 . 我已经实施了Google混合服务器幻灯片流(离线访问),以便在用户离线时代表此用户使用Google服务 .

我将刷新令牌存储在我的数据库中,并使用它来刷新访问令牌 . 使用访问令牌,我可以查询API . 例如,以下代码 correctly returns "myapp"驱动器上的文件:

// Get the API client

$client = new Google_Client();
$client->setClientId($this->clientId);
$client->setClientSecret($this->clientSecret);
$client->setAccessType('offline');
...
$client->addScope([
    'https://spreadsheets.google.com/feeds',
    'https://docs.google.com/feeds',
    Google_Service_Drive::DRIVE
]);

// Construct the service object

$service = new Google_Service_Drive($client);
$params = array(
    'pageSize' => 10,
    'fields' => "nextPageToken, files(id, name)"
);
$results = $service->files->listFiles($params);

foreach ($results->getFiles() as $file) {
    printf("%s (%s)\n", $file->getName()); // OK
}

...... works fine!

其他用户将一些文件共享给“myapp” .

Now I would like to get content of a shared Spreadsheet:

$fileId = "1GRTldB2....";
$result = $service->files->get($fileId, [
    'fields' => 'name,md5Checksum,size,createdTime,modifiedTime,ownedByMe,properties,shared,sharedWithMeTime,webContentLink,webViewLink'
]);

$url = $result['webViewLink'];
//$url = 'https://www.googleapis.com/drive/v3/files/'.$fileId.'?alt=media';

$method = 'GET';
$headers = ["Authorization" => "Bearer $accessToken", "GData-Version" => "3.0"];
$httpClient = new GuzzleHttp\Client(['headers' => $headers]);

$resp = $httpClient->request($method, $url);
$body = $resp->getBody()->getContents();
$code = $resp->getStatusCode();
$reason = $resp->getReasonPhrase();
echo "$code : $reason\n\n";
echo "$body\n";

This code gives an error:

致命错误:未捕获异常'GuzzleHttp \ Exception \ ClientException',消息'在C:\ wamp \ www \ core \ vendor \ guzzlehttp \ guzzle \ src \ Exception \ RequestException.php第107行(!)GuzzleHttp \ Exception \ ClientException :客户端错误:GET https://www.googleapis.com/drive/v3/files/1GRTldB2KDFGmFZgFST28-MaHKs7y7eqelbzDpdxuJBg?alt=media导致401 Unauthorized响应:{“error”:{“errors”:[{“domain” :“全局”,“原因”:“authError”,“消息”:“无效凭据”(截断...)在C:\ wamp \ www \ core \ vendor \ guzzlehttp \ guzzle \ src \ Exception \ RequestException.php在107号线上

authError / InvalidCredentials

有任何想法吗?

1 回答

  • 0

    共享工作表...是否与您在Google API上使用的“用户”共享?您知道当您右键单击实际文档并说“共享...”时

相关问题