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?
My Answer:
It seems like the issue might be related to the scopes you are setting for the client. The scopes you have set for the Forms API might not be correct or sufficient for the operation you are trying to perform.
You can try setting the scopes specifically for the Forms API like this:
php
$client->setScopes([
\Google\Service\Forms::FORMS,
\Google\Service\Forms::FORMS_READONLY,
]);
Make sure to check the official Google Forms API documentation for the correct scopes required for the specific operation you are trying to perform.
If the issue persists, you can also check the permissions for the service account in the Google Cloud Console. Make sure that the service account has the necessary permissions to access the Forms API.
You can find the permissions for the service account in the Google Cloud Console under IAM & Admin > IAM. Search for the service account email and make sure it has the appropriate roles assigned for accessing the Forms API.
If you are still facing issues, you can try regenerating the service account key and updating the credentials in your code.
If none of the above solutions work, you may need to handle the OAuth flow manually for the Forms API. This would involve obtaining an access token using the service account credentials and including it in the request headers.
I hope this helps you resolve the issue with authenticating the Google Forms API using a service account. Let me know if you have any further questions.
Rate this post
4 of 5 based on 8962 votesComments