首页 > 解决方案 > 凭据无效 - Google Drive API

问题描述

下面的 api 调用总是返回无效的凭据

$ch = curl_init();
$fileId = 'myfile_id';
$key = 'apikey';
$accessToken = 'access_token'

curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/drive/v2/files/'.$fileId.'?key='.$key);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');

$headers = array();
$headers[] = 'Authorization: Bearer '.$accessToken;
$headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
print_r($result);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

回复

{ "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } } 

我在收到访问令牌后立即调用 api。所以我确定它没有过期并且api密钥也是正确的。

有人对此有任何想法吗?

标签: phpgoogle-drive-apigoogle-apis-explorer

解决方案


推荐阅读