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

标签 php google-api google-drive-api google-api-php-client

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

我正在使用 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/

相关文章:

php - 无法在 PHP 中显示 session 消息?

javascript - Ajax获取输入文本框数组并将结果显示到div

python - 有没有办法使用 BigQuery 从 Google Analytics 导出原始数据?

youtube - 哪种方式存储 Youtube API 数据是合法的

python - Colaboratory - 记住 Google Drive Auth(Python)

php - 在 PHP 中验证后数据未插入数据库

php - Zend Auth 和 ACL

google-api - 使用 Python Google API 进行身份验证以访问自己的帐户的最简单方法

java - 将本地目录中的所有文件上传到 Google Drive (Google Drive API V3)

java - 使用谷歌云存储的文档管理系统