【问题标题】:Google Drive API v2 downloadUrl isn't being properly authenticatedGoogle Drive API v2 downloadUrl 未经过正确身份验证
【发布时间】: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


    【解决方案1】:

    所以,在检查Drive v2 PHP Quickstart

    PHP 客户端库不再支持 Drive v2。见 Drive v3 PHP Quickstart 代替。

    要从 Drive v3 开始,您需要 ff。

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

    【讨论】:

    • 啊,是的,为什么它不起作用是有道理的。但是 Drive v3 PHP 快速入门在我的情况下对我没有帮助。我正在运行一个无法访问控制台的 Web 服务器。我想要做的是使用服务帐户并且根本没有得到用户的同意。然后使用服务帐户将文件下载到用户的 PC。除了下载的文件不是正确的文件名之外,我可以使用驱动器 api v3 执行此操作。这就是为什么我想使用驱动器 v2 中的 downloadUrl。我可以补充一下,这个网址完全完成了我想要做的事情:links-safety.com/download.php?id={FILE-ID}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多