简体   繁体   English

PHP Google API 客户端 Forms 服务凭证

[英]PHP Google API Client Forms Service Credentials

I'm trying to use the Google Forms API with the google/apiclient composer package while authenticating with a service account like so...我正在尝试将 Google Forms API 与 google/apiclient 作曲家 package 一起使用,同时使用像这样的服务帐户进行身份验证...

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')
            

I have used this same service account to successfully use the Google Drive API in a very similar manner in the same codebase.我已经使用相同的服务帐户在相同的代码库中以非常相似的方式成功使用了 Google Drive API。 My issue is that, for the Forms API, the client doesn't seem to be handling authentication automatically like it does when using the Drive API.我的问题是,对于 Forms API,客户端似乎没有像使用 Drive API 时那样自动处理身份验证。 The response I get back from the API on the above forms.get request is the following:我从上述forms.get请求的 API 得到的响应如下:

  {
  "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"
        }
      }
    ]
  }
}

My composer.json file contains this entry:我的 composer.json 文件包含以下条目:

"extra": {     
    "google/apiclient-services": [
        "Calendar",
        "Drive",
        "Gmail",
        "Directory",
        "Forms"
    ]
},

The forms API is enabled for the workspace account that the service account is associated with. forms API 已为服务帐户关联的工作区帐户启用。 How do I fix this issue?我该如何解决这个问题? Do I need to perform the OAuth steps myself for the forms API OR is this just a misleading error message and I'm missing some kind of permission?我是否需要自己为 forms API 执行 OAuth 步骤,或者这只是一个误导性错误消息,我错过了某种许可? If I'm possibly missing a permission, which ones should I check and where can I find them in the google developer console?如果我可能缺少权限,我应该检查哪些权限以及在谷歌开发者控制台中的哪里可以找到它们?

As per the information provided it seems may be you haven't added Scopes for google Forms API in OAuth Consent.根据提供的信息,您似乎没有在 OAuth 同意中添加 Google Forms API 的范围。 go to google cloud console and OAuth Consent Screen page there in second step you will see scopes option to add/remove check for Google Form API scopes added or not. go 到谷歌云控制台和 OAuth 同意屏幕页面在第二步中,您将看到添加/删除 Google 表单 API 范围检查的范围选项。 if not then add and then it will work fine.如果没有,则添加,然后它会正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM