php - Google Drive SDK 服务帐户错误 "The authenticated user has not installed the app with client id ..."

标签 php google-drive-api

我正在尝试创建一个可以在后台运行的 Google 云端硬盘应用程序(例如 cronjob),无需任何用户交互,使用 Google Drive SDK 服务帐户,但它给了我这个错误,我不明白为什么:

Error calling POST https://www.googleapis.com/upload/drive/v1/files?uploadType=multipart: (403) The authenticated user has not installed the app with client id {my_client_id}

在 Google API 控制台上,我激活了 Drive API 和 Drive SDK。 在 Drive SDK 选项卡上,我已经设置了所有必需的信息。 我还将我的应用程序仅供测试人员发布到 Chrome 网上应用店,并将其安装到我的 Google Chrome 中,应用程序名称出现在我的 Google 云端硬盘“创建”菜单上。

这是我的代码片段:

<?php

require_once dirname(__FILE__).'/google-api-php-client/src/apiClient.php';
require_once dirname(__FILE__).'/google-api-php-client/src/contrib/apiDriveService.php';

$apiClient = new apiClient();
$apiClient->setClientId(DRIVE_CLIENT_ID);
$apiClient->setClientSecret(DRIVE_CLIENT_SECRET);
$apiClient->setAssertionCredentials(new apiAssertionCredentials(
    OAUTH2_EMAIL, 
    array('https://www.googleapis.com/auth/drive.file'), 
    file_get_contents(SERVICE_ACCOUNT_PRIVATEKEY_FILE))
);

$apiDriveService = new apiDriveService($apiClient);

$file = new DriveFile();
$file->setTitle('filename.txt');
$file->setMimeType('text/plain');
$createdFile = $apiDriveService->files->insert($file, array(
    'data' => 'file content goes here....',
    'mimeType' => 'text/plain'
));

?>

据我了解,应用程序可以代表用户使用 Google Drive SDK 的服务帐户。这是否意味着用户没有身份验证问题(权限请求)?那么应用程序如何验证自己呢?或者我的理解可能是错误的?请这边帮忙开导。

最佳答案

由于其安全模型,Drive SDK 不支持服务帐户。

我的建议是使用您的应用程序从云端硬盘网络用户界面打开(或创建)一个文件,并存储您在 OAuth 2.0 授权流程完成后获得的检索到的访问和刷新 token 。

从您的 cron 作业中,只需检索这些凭据即可代表您的用户向 Drive API 发送授权请求。

关于php - Google Drive SDK 服务帐户错误 "The authenticated user has not installed the app with client id ...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10426632/

相关文章:

php - 使用 HTML5 文件 API 在 PHP 服务器上上传 .xls 文件

php - 在表单输入中回显 php 作为带有路径的按钮

oauth-2.0 - 使用包含 access_token 的 Uri 从 Google 文档下载

python - 使用 Drive API 编辑 Google 文档

java - 将大文件插入 Google 驱动器时出现 IOException "insufficient data written"

php - 我可以隐藏损坏的图像吗?

PHP - 将多张照片上传到数据库

Php 网页无法显示带有 Wamp 服务器的产品列表

java - 使用java api将文件上传到Google Drive

python - unauthorized_client : Client is unauthorized to retrieve access tokens using this method