Php 客户端未经授权使用此方法检索访问令牌,或者客户端未被授权使用任何请求的作用域
你能帮我做这个吗 我使用的是PHP谷歌API V2.2.2 我的代码使用为其创建的gserviceaccount工作,但是,当我试图在我的个人GOOGLE驱动器上操作文件时,我出现了这个错误 我尝试了以下步骤: 一切进展顺利,我的项目在我的网站上展示 以下是我在谷歌硬盘中创建文件夹的代码:Php 客户端未经授权使用此方法检索访问令牌,或者客户端未被授权使用任何请求的作用域,php,google-api,google-drive-api,google-api-php-client,Php,Google Api,Google Drive Api,Google Api Php Client,你能帮我做这个吗 我使用的是PHP谷歌API V2.2.2 我的代码使用为其创建的gserviceaccount工作,但是,当我试图在我的个人GOOGLE驱动器上操作文件时,我出现了这个错误 我尝试了以下步骤: 一切进展顺利,我的项目在我的网站上展示 以下是我在谷歌硬盘中创建文件夹的代码: function NovaPasta() { require_once APPPATH . 'third_party/google-api-php-client-2.2.2/vendor/au
function NovaPasta() {
require_once APPPATH . 'third_party/google-api-php-client-2.2.2/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS='. APPPATH . 'third_party/google-api-php-client-2.2.2/Asist-428f3540df9a.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
//$client->setAuthConfig(APPPATH . 'third_party/google-api-php-client-2.2.2/Asist-428f3540df9a.json');
$client->addScope("https://www.googleapis.com/auth/drive");
$client->setSubject('xxxx@gmail.com');
$driveService = new Google_Service_Drive($client);
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => 'new folder name',
'mimeType' => 'application/vnd.google-apps.folder'));
$file = $driveService->files->create($fileMetadata, array(
'fields' => 'id'));
printf("Folder ID: %s\n", $file->id);
}
以下是错误:
遇到未捕获的异常
类型:谷歌服务例外
消息:{“error”:“unauthorized_client”,“error_description”:“client无权使用此方法检索访问令牌,或者client未被授权用于请求的任何作用域。”}
如果要使用服务帐户,请取消注释$client->setAuthConfig指向json文件并删除行$client->useApplicationDefaultCredentials() 查看更多示例,请访问
您是否仅尝试访问您的驱动器帐户?除了您自己的帐户之外,您永远不会访问其他用户帐户吗?您能告诉我们您使用了哪些作用域吗?
$client = new Google_Client();
$client->setAuthConfig('service-account.json');
$client->setScopes(['https://www.googleapis.com/auth/drive']);
$service = new Google_Service_Drive($client);
$fileMetadata = new Google_Service_Drive_DriveFile(array('name' => 'Google_Service_Folder','mimeType' => 'application/vnd.google-apps.folder'));
$file = $service->files->create($fileMetadata, array('fields' => 'id'));