我使用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();
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?
2017年2月21日更新:错误不再是间歇性的,每次都会发生错误,因此我无法再通过服务帐户连接到常规帐户.
2017年3月10日更新:好像这是用户的误解.必须为服务帐户授予"域范围权限"才能冒充该域的用户 - 这是我尚未做过的事情.我只是授权客户端访问此处所述的用户帐户.