【问题标题】:Google API Service Account Authorization ErrorGoogle API 服务帐号授权错误
【发布时间】:2017-06-13 16:50:56
【问题描述】:

我正在使用google-api-php-client 客户端通过服务帐户连接到 Google 云端硬盘。大多数情况下它都有效,但每隔一段时间(例如,如果我坐在这里一遍又一遍地刷新页面,它是每 5 到 10 次),我会收到带有消息 unauthorized_client: Client is unauthorized to retrieve access tokens using this methodGoogle_Service_Exception。仅当存在 $this->drive_service->files->listFiles() 代码时才会出现该错误。如果我直接使用服务帐户而不是使用setSubject() 模拟其他用户,则不会发生这种情况。

        $this->client = new Google_Client();
        $this->client->useApplicationDefaultCredentials();
        $this->client->addScope("https://www.googleapis.com/auth/drive");
        $this->client->setSubject('xxxx');
        $this->drive_service = new Google_Service_Drive($this->client);
        $files = $this->drive_service->files->listFiles();

有什么想法吗?

2017 年 2 月 21 日更新:该错误不再是间歇性的,它每次都会发生,所以我再也无法通过服务帐户连接到普通帐户。

2017 年 3 月 10 日更新:这似乎是用户的误解。服务帐户必须被授予“域范围的权限”才能模拟该域的用户——我没有这样做。我只是授权客户访问用户帐户,如here 所述。

【问题讨论】:

    标签: google-api google-api-php-client service-accounts


    【解决方案1】:

    我不知道为什么您的错误会定期发生,但我的理解是要冒充用户,您必须grant the service account access

    【讨论】:

      【解决方案2】:

      我在新创建被模拟的帐户时间歇性地看到此错误。从表面上看,在尝试创建服务之前添加 10 秒延迟可以解决问题。

      【讨论】:

      • 被模拟的用户帐户不是新创建的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-21
      • 2014-07-19
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 2020-07-03
      相关资源
      最近更新 更多