googleads / google-ads-php

Google Ads API Client Library for PHP
https://developers.google.com/google-ads/api/docs/client-libs/php
Apache License 2.0
295 stars 262 forks source link

using oauth2 service account flow #947

Closed devzer01 closed 1 year ago

devzer01 commented 1 year ago

i am using the latest version of the php client library from master (running the example code) i have google_ads_php.ini placed in my home directory with the following contents

(venv) nayana@nayana-Stealth-15M-A11SEK:~/code/google-ads-php/examples/BasicOperations$ cat ~/google_ads_php.ini 
; For service account flow.
jsonKeyFilePath = "/home/nayana/code/keywordapp-backend/managed-client-portal-4176c1bf1e30.json"
scopes = "https://www.googleapis.com/auth/adwords"
impersonatedEmail = "analytics@xyz.com

and then I run the example basic operations get campaign I see the following error message

php GetCampaigns.php
PHP 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
Stack trace:
#0 /home/nayana/code/google-ads-php/src/Google/Ads/GoogleAds/Lib/OAuth2TokenBuilder.php(161): Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder->validate()
#1 /home/nayana/code/google-ads-php/examples/BasicOperations/GetCampaigns.php(51): Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder->build()
#2 /home/nayana/code/google-ads-php/examples/BasicOperations/GetCampaigns.php(131): Google\Ads\GoogleAds\Examples\BasicOperations\GetCampaigns::main()
#3 {main}
  thrown in /home/nayana/code/google-ads-php/src/Google/Ads/GoogleAds/Lib/OAuth2TokenBuilder.php on line 216

I examine the validate function in class OAuth2TokenBuilder as follows

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."
            );
        }
    }

it appears that the test for clientId, clientSecret is lacking to test if key file is not set. because based on what I understand the clientId test will always fail even with the keyfile is set.

or am I doing something wrong?

will wait for your response thanks

fiboknacky commented 1 year ago

It's an elseif so if you provide either jsonKeyFilePath or scopes, it should fall into the first if without going to the second one. Do you retain the format (including all the section name) of the google_ads_php.ini file? It looks like you cut it short. Could you confirm?

devzer01 commented 1 year ago

hi thanks for your response, you are right there is no section name in my .ini file. what should the section be called? i followed the documentation here which didn't have a section https://developers.google.com/google-ads/api/docs/client-libs/php/oauth-service

fiboknacky commented 1 year ago

Please copy this example file and comment/uncomment the keys accordingly. Thanks.