【发布时间】:2018-01-14 05:45:28
【问题描述】:
我使用访问令牌发出授权请求后返回的 downloadUrl 属性有问题。如果我尝试在浏览器中使用该 downloadUrl,它将给出 HTTP 错误 403。我已经仔细检查过,我的访问令牌是有效的,我可以通过这种方式请求文件元数据,这就是我能够得到的方式首先是downloadUrl。 这是我现在的代码:
<?php
require_once('vendor/autoload.php');
include_once "examples/templates/base.php";
$client = new Google_Client();
$credentials_file = __DIR__.'/credentials.json';
putenv('GOOGLE_APPLICATION_CREDENTIALS='.$credentials_file);
if ($credentials_file = checkServiceAccountCredentialsFile()) {
// set the location manually
$client->setAuthConfig($credentials_file);
} elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
// use the application default credentials
$client->useApplicationDefaultCredentials();
} else {
echo missingServiceAccountDetailsWarning();
exit;
}
$client->setApplicationName('test-debug');
$client->setScopes(array('https://www.googleapis.com/auth/drive.readonly'));
$service = new Google_Service_Drive($client);
$id = "some-id-here";
$pre = "https://www.googleapis.com/drive/v2/files/";
$tokenArray = $client->fetchAccessTokenWithAssertion();
$accessToken = $tokenArray["access_token"];
$content = json_decode(file_get_contents($pre.$id."?access_token=".$accessToken));
$downloadUrl = $content->{'downloadUrl'};
echo $downloadUrl;
?>
使用此代码,我可以成功地向 Google 的 API 发出请求并检索 downloadUrl 参数。唯一的问题是,由于某种原因它无效。有人会确切知道为什么会这样吗?它与我如何使用访问令牌发出请求有关吗?如果你认为你有这个问题的答案,请加入。
P.S.我还想提一下,通过使用 php cURL 会返回错误 401 而不是错误 403
【问题讨论】:
标签: php api google-drive-api