gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:15:11 24 4
gpt4 key购买 nike

我正在尝试创建一个可以在后台运行的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com