调用POST错误 https://www.googleapis.com/upload/drive/v1/files?uploadType=multipart :(403)经过身份验证的用户未安装带有客户端ID的应用程序{my_client_id } b


在Google API控制台上,我已经激活了Drive API和Drive SDK。
在Drive SDK选项卡上,我已经设置了所有必需的信息。
我也将我的应用程序仅发布到Chrome网上应用店,并将其安装到我的谷歌浏览器中,应用程序名称显示在我的Google云端硬盘创建菜单中。



这是我的代码片段:

 <?php 

require_once目录名(FILE __ __) '/谷歌-API的PHP的客户端/ 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'=>'文件内容到这里....',
'mimeType '=>'text / plain'
));

?>

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

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

我建议使用您的应用程序在Drive web UI中打开(或创建)一个文件,并存储您在OAuth 2.0之后获得的检索到的访问和刷新令牌授权流程已完成。

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


I am trying to create a Google Drive app that can run in the background (e.g. a cronjob), without any user interaction, using Google Drive SDK Service Account, but it gives me this error and I can't understand why:

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}

On Google APIs Console, I have activated Drive API and Drive SDK. On the Drive SDK tab, I have setup all the required information. I also have published my app for testers only to the Chrome Web Store, and installed it into my Google Chrome and the application name appears on my Google Drive "Create" menu.

This is a snippet of my code:

<?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'
));

?>

From what I understand, an application can use Service Account for Google Drive SDK on behalf of a user. Does that mean that there is no authentication question (request for permissions) to the user? Then how does the application authenticate itself? Or my understanding is probably wrong? Please help to enlighten me over here.

Service Accounts is not supported by the Drive SDK due to its security model.

What I would suggest is to open (or create) a file from the Drive web UI with your application and store the retrieved access and refresh tokens you get after the OAuth 2.0 authorization flow has completed.

From your cron job, simply retrieve those credentials to send authorized requests to the Drive API on behalf of your user.

这篇关于Google Drive SDK服务帐户错误“经过身份验证的用户未安装带有客户端ID的应用程序...”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!