【发布时间】:2017-06-13 16:50:56
【问题描述】:
我正在使用google-api-php-client 客户端通过服务帐户连接到 Google 云端硬盘。大多数情况下它都有效,但每隔一段时间(例如,如果我坐在这里一遍又一遍地刷新页面,它是每 5 到 10 次),我会收到带有消息 unauthorized_client: Client is unauthorized to retrieve access tokens using this method 的 Google_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