PHP Google API服务帐户

时间:2017-02-03 10:16:07

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

我正在努力在冒充用户时访问google api。

我可以作为该用户连接但是我收到错误,我收到错误:

"error": {
    "errors": [
         {
             "domain": "global",
             "reason": "appNotAuthorizedToFile",
             "message": "The user has not granted the app <app id> write access to the file <file id>",
             "locationType": "header",
             "location": "Authorization"
         }
    ],
}

推荐的解决方案是使用Google Picker并让用户允许我的应用访问此文件。但是,当我使用具有域范围授权和g-suite启用的服务帐户时,我认为这不是必需的。

我目前的PHP代码是:

$client = new Google_Client();
$client->setApplicationName($appName);
$client->setAuthConfig($credentials);
$client->addScope($scopes);
$client->setClientId($key);
$client->useApplicationDefaultCredentials();
$client->setSubject($userTobe);
$client->setAccessType('offline');

我正在使用putenv将service_account.json推送到我的文件中。我按照它的建议快速启动了。我已在安全控制台中启用了所有必需的范围。如果我指定getRefreshTokenwithAssertion(),我可以从 ya29开始获取access_token。但是这仍然会出现上述错误。

我目前无法从这里离开。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

您使用的是哪个范围?请确保您在应用中设置了驱动器验证范围:https://www.googleapis.com/auth/drive(Google_service_Drive :: DRIVE)

答案 1 :(得分:0)

客户端和范围是否已在管理控制台中获得授权?

admin console =&gt; security =&gt;高级设置=&gt;管理API客户端访问