PHP Google API 服务帐户

PHP Google API service accounts

我目前正在努力在模拟用户时访问 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 但是这仍然会出现上面的错误。

我目前不知道从哪里开始。任何帮助将不胜感激。

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

管理控制台=>安全=>高级设置=>管理API客户端访问

您使用的是哪些示波器?请确保您在应用程序中设置驱动器授权范围:https://www.googleapis.com/auth/drive (Google_service_Drive::DRIVE)