我试图在google/apiclient composer包中使用Google,同时使用这样的服务帐户进行身份验证.
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . storage_path('google-api-credentials' . DIRECTORY_SEPARATOR . 'service-account.json'));
$client = new \Google_Client();
$client->setScopes([
\Google\Service\Drive::DRIVE,
\Google\Service\Drive::DRIVE_FILE,
\Google\Service\Drive::DRIVE,
\Google\Service\Drive::DRIVE_READONLY,
\Google\Service\Forms::FORMS_BODY,
\Google\Service\Forms::FORMS_BODY_READONLY,
\Google\Service\Forms::FORMS_RESPONSES_READONLY,
]);
$client->useApplicationDefaultCredentials(true);
$client->setIncludeGrantedScopes(true);
$client->setAccessType('offline');
$service = new \Google_Service_Forms($client);
$service->forms->get('some valid form id')
我使用了相同的服务帐户,以非常类似的方式在相同的代码基中成功地使用了Google。我的问题是,对于Forms API,客户端似乎并不像使用Drive时那样自动处理身份验证。我从API获得的关于上述forms.get请求的响应如下:
{
"error": {
"code": 401,
"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"errors": [
{
"message": "Login Required.",
"domain": "global",
"reason": "required",
"location": "Authorization",
"locationType": "header"
}
],
"status": "UNAUTHENTICATED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "CREDENTIALS_MISSING",
"domain": "googleapis.com",
"metadata": {
"method": "google.apps.forms.v1.FormsService.GetForm",
"service": "forms.googleapis.com"
}
}
]
}
}
我的composer.json文件包含以下条目:
"extra": {
"google/apiclient-services": [
"Calendar",
"Drive",
"Gmail",
"Directory",
"Forms"
]
},
窗体API为与服务帐户关联的工作区帐户启用。我该如何解决这个问题?我需要自己为forms执行OAuth步骤吗?还是这只是一条误导性错误消息,而我缺少某种权限?如果我可能没有权限,我应该检查哪些权限,在google控制台中可以在哪里找到它们?
发布于 2022-08-29 22:26:26
根据提供的信息,您可能还没有在OAuth同意中为google添加。进入和OAuth同意屏幕页面,在第二步中,您将看到范围选项添加/删除作用域是否已添加。如果没有,那么添加,然后它将工作良好。
https://stackoverflow.com/questions/73506348
复制