繁体   English   中英

Google Drive API v2 downloadUrl未正确通过身份验证

[英]Google Drive API v2 downloadUrl isn't being properly authenticated

我对带有访问令牌的授权请求后返回的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参数。 唯一的问题是,由于某种原因它是无效的。 谁能确切知道为什么会这样? 它与我如何使用访问令牌发出请求有关吗? 如果您认为自己有解决此问题的方法,请发出提示。

PS我还想提到,通过使用php cURL,将返回错误401而不是错误403

因此,在检查Drive v2 PHP Quickstart之后

PHP客户端库不再支持Drive v2。 请参阅Drive v3 PHP快速入门

要在Drive v3中启动,您需要ff。

  • PHP 5.4或更高版本,并安装了命令行界面(CLI)和JSON扩展。
  • Composer依赖性管理工具。
  • 访问互联网和网络浏览器。
  • 启用了Google云端硬盘的Google帐户。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM