1

I'm trying to use the Google Forms API with the google/apiclient composer package while authenticating with a service account like so...

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. 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. The response I get back from the API on the above forms.get request is the following:

  {
  "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:

"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. 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? If I'm possibly missing a permission, which ones should I check and where can I find them in the google developer console?

1 Answer 1

2

As per the information provided it seems may be you haven't added Scopes for google Forms API in OAuth Consent. 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. if not then add and then it will work fine.

1
  • Thank you. You're right, I was missing the forms related scopes for the service account in the second step of the OAuth Consent. Aug 30, 2022 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.