gpt4 book ai didi

php - 客户端无权使用此方法检索访问 token ,或者客户端无权访问任何请求的范围

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

你能帮我解决这个问题吗?

我正在使用 PHP GOOGLE API V2.2.2

我的代码使用为其创建的 gserviceaccount 工作,但是,当我尝试操作我个人的 GOOGLE DRIVE 上的文件时,我遇到了这个错误...

我尝试按照此处显示的步骤操作:Github issue 801

一切顺利,我的项目显示在我的 authorized client list

这是我在我的谷歌驱动器中创建文件夹的代码:

  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);
}

这里是错误:

An uncaught Exception was encountered Type: Google_Service_Exception

Message: { "error": "unauthorized_client", "error_description": "Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested." }

enter image description here

最佳答案

如果你想使用服务帐户然后取消注释 $client->setAuthConfig 指向 json 文件并删除行 $client->useApplicationDefaultCredentials();

https://github.com/googleapis/google-api-php-client/tree/master/examples 查看更多示例

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

关于php - 客户端无权使用此方法检索访问 token ,或者客户端无权访问任何请求的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55821895/

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