Cannot get OAuth credentials be recognized by PHP script

114 views
Skip to first unread message

M T

unread,
Jan 23, 2025, 7:02:07 PM1/23/25
to Google Ads API and AdWords API Forum
Hello,
Im trying to write a PHP script to check historical data of avg monthly searches for a given keywords list. However I cannot get the credential files to work.

// planner.php
use Google\Ads\GoogleAds\Lib\V18\GoogleAdsClientBuilder;
use Google\Ads\GoogleAds\V18\Services\GenerateKeywordHistoricalMetricsRequest;

require 'vendor/autoload.php';

$configFilePath = 'google_ads_php.ini';

$googleAdsClient = (new GoogleAdsClientBuilder())
    ->fromFile($configFilePath)
    ->build();

$customerId = '9810123038';

try {
    $keywordPlanService = $googleAdsClient->getKeywordPlanIdeaServiceClient();
    $keywords = ['test keyword', 'another keyword'];

    $request = new GenerateKeywordHistoricalMetricsRequest([
        'keywords' => $keywords
    ]);

    $response = $keywordPlanService->generateKeywordHistoricalMetrics($request);

    foreach ($response->getMetrics() as $metric) {
        printf(
            "Keyword: %s, Avg monthly volume: %d\n",
            $metric->getText(),
            $metric->getMetrics()->getAvgMonthlySearches()
        );
    }
} catch (Exception $e) {
    printf("Error: %s\n", $e->getMessage());
}

in the same folder as code above I have 2 files:
// google_ads_php.ini

[GOOGLE_ADS]
developerToken = "cut"
jsonKeyFilePath = "E:/path/to/credentials.json"
clientSecret = "cut"
refreshToken = "cut"
Ive got the refreshToken from buildFullAuthorizationUri() that prompted google login screen correctly.
and:
// credentials.json

{"web":
{"client_id":"cut",
"project_id":"cut",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
"client_secret":"cut",
"redirect_uris":["http://localhost"]}}

No matter what I do I cannot go over:
Fatal error: Uncaught InvalidArgumentException: OAuth2 authentication credentials must not be null.
Stack trace: #0
Google\Ads\GoogleAds\Lib\V18\GoogleAdsClientBuilder->validate()
Google\Ads\GoogleAds\Lib\V18\GoogleAdsClientBuilder->build()
thrown in  google-ads-php\src\Google\Ads\GoogleAds\Lib\V18\GoogleAdsClientBuilder.php on line 429

Any ideas on how to solve that? File paths are correct because I can print those files content.

Google Ads API Forum Advisor

unread,
Jan 23, 2025, 11:55:58 PM1/23/25
to alvar...@gmail.com, adwor...@googlegroups.com
Hi,

Thank you for reaching out to the Google Ads API support team.

I would recommend you refer to this guide on 'Make an API call' for the step-by-step instructions on how to make an API call using the PHP client library. This guide also has information on how to fetch a refresh token using the credentials.json file that was downloaded after you created the client ID and client secret.

I hope this helps.

 
This message is in relation to case "ref:!00D1U01174p.!5004Q02vGyCT:ref" (ADR-00285700)

Thanks,
 
Google Logo Google Ads API Team

Feedback
How was our support today?

rating1    rating2    rating3    rating4    rating5



M T

unread,
Jan 24, 2025, 7:24:49 PM1/24/25
to Google Ads API and AdWords API Forum
Hey,
I did exactly that, but withOAuth2Credential shows as undefined:
namespace Google\Ads\GoogleAds\Examples\Planning;
    header("Content-type: text/plain; charset=utf-8");

require 'vendor/autoload.php';
use GetOpt\GetOpt;
use Google\Ads\GoogleAds\Examples\Utils\ArgumentNames;
use Google\Ads\GoogleAds\Examples\Utils\ArgumentParser;
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\Ads\GoogleAds\Lib\V18\GoogleAdsClient;
use Google\Ads\GoogleAds\Lib\V18\GoogleAdsClientBuilder;
use Google\Ads\GoogleAds\Lib\V18\GoogleAdsException;
use Google\Ads\GoogleAds\Util\V18\ResourceNames;
use Google\Ads\GoogleAds\V18\Common\MonthlySearchVolume;
use Google\Ads\GoogleAds\V18\Enums\KeywordPlanCompetitionLevelEnum\KeywordPlanCompetitionLevel;
use Google\Ads\GoogleAds\V18\Enums\KeywordPlanNetworkEnum\KeywordPlanNetwork;
use Google\Ads\GoogleAds\V18\Enums\MonthOfYearEnum\MonthOfYear;
use Google\Ads\GoogleAds\V18\Errors\GoogleAdsError;
use Google\Ads\GoogleAds\V18\Services\GenerateKeywordHistoricalMetricsRequest;
use Google\Ads\GoogleAds\V18\Services\GenerateKeywordHistoricalMetricsResult;
use Google\ApiCore\ApiException;


$oAuth2Credential = (new OAuth2TokenBuilder())
    ->fromFile('google_ads_php.ini')
    ->build();

$googleAdsClient = (new GoogleAdsClientBuilder())
    ->fromFile('google_ads_php.ini')
    ->withOAuth2Credential($oAuth2Credential)
    ->build();


Tried updating packages, manually downloading them from github, nothing works.

Google Ads API Forum Advisor

unread,
Jan 26, 2025, 9:09:47 PM1/26/25
to alvar...@gmail.com, adwor...@googlegroups.com
Hi,

As the issue is related to the PHP client library, I would suggest you create a "New issue" with the developer who maintains the PHP client library using this link.
Reply all
Reply to author
Forward
0 new messages