Google API服务帐户授权错误

时间:2017-01-27 21:48:58

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

我正在使用google-api-php-client客户端使用服务帐户连接到Google云端硬盘。它大部分时间都有效,但每隔一段时间(例如,如果我坐在这里每隔5到10次刷新一次页面),我会收到Google_Service_Exception消息unauthorized_client: Client is unauthorized to retrieve access tokens using this method。只有$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所述的用户帐户。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我新创建了被模拟的帐户时间歇性地看到此错误。从它的外观来看,在尝试创建服务之前添加10秒的延迟消除了这个问题。