unable to authenticate using service account json credentials in php lib

14 views
Skip to first unread message

Nayana Hettiarachchi

unread,
Sep 7, 2023, 6:02:41 AM9/7/23
to Google Ads API and AdWords API Forum
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Hi,

i have my google_ads_php.ini file look as follows

jsonKeyFilePath = "/home/nayana/code/keywordapp-backend/managed-client-portal-4176c1bf1e30.json"
scopes = "https://www.googleapis.com/auth/adwords"
impersonatedEmail = "anal...@xxxxx.com"

and when I use
oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();

i get the following error

HP Fatal error:  Uncaught UnexpectedValueException: All of 'clientId', 'clientSecret', and 'refreshToken' must be set when using installed/web application flow. in /home/nayana/code/google-ads-php/src/Google/Ads/GoogleAds/Lib/OAuth2TokenBuilder.php:216
 

i followed instruction on service account flow. anyone have experience on this?

Nayana

Nayana Hettiarachchi

unread,
Sep 7, 2023, 6:45:04 AM9/7/23
to Google Ads API and AdWords API Forum
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
public function validate()
{
if (
(!is_null($this->jsonKeyFilePath) || !is_null($this->scopes))
&& (!is_null($this->clientId) || !is_null($this->clientSecret)
|| !is_null($this->refreshToken))
) {
throw new InvalidArgumentException(
'Cannot have both service account '
. 'flow and installed/web application flow credential values set.'
);
}
if (!is_null($this->jsonKeyFilePath) || !is_null($this->scopes)) {
if (is_null($this->jsonKeyFilePath) || is_null($this->scopes)) {
throw new InvalidArgumentException(
"Both 'jsonKeyFilePath' and "
. "'scopes' must be set when using service account flow."
);
}
} elseif (
is_null($this->clientId)
|| is_null($this->clientSecret)
|| is_null($this->refreshToken)
) {
throw new UnexpectedValueException(

"All of 'clientId', 'clientSecret', and 'refreshToken' must be set when using "
. "installed/web application flow."
);
}
}
based on the code above the service account oauth2 flow doesn't seem to work because it tries to always check for the presence of clientId etc

Google Ads API Forum Advisor

unread,
Sep 7, 2023, 7:52:32 AM9/7/23
to nay...@gmail.com, adwor...@googlegroups.com

Hello Nayana,

Greetings from the Google Ads API Team.

Please note that according to this API documentation(https://developers.google.com/google-ads/api/docs/client-libs/php/oauth-service#set_up_client_library_with_json_file), the following fields need to be set when using the service account flow:

  • jsonKeyFilePath
  • scopes
  • impersonatedEmail

Since you have already set these fields, can you confirm if you have commented out clientIdclientSecret, and refreshToken in your configuration file? It is stated here in the file under [OAUTH2] that(https://github.com/googleads/google-ads-php/blob/8372d9f7551240e70fbe569a12d7e12f8fe0c324/examples/Authentication/google_ads_php.ini), “Required OAuth2 credentials. Uncomment and fill in the values for the appropriate flow based on your use case”. Since your flow is for service accounts, can you please try commenting out these lines and running your code again?

If you have any further questions relating to the configuration file, I would suggest contacting the PHP client library owner on github via their Issues Tab for more insights on your issue as they are more equipped to answer inquiries relating to their client library. You may contact them via the link below:

This message is in relation to case "ref:_00D1U1174p._5004Q2oj8Sh:ref"

Thanks,
 
Google Logo Google Ads API Team


Reply all
Reply to author
Forward
0 new messages