Google Ads PHP

303 views
Skip to first unread message

Mike Scola

unread,
Apr 29, 2024, 5:38:41 PM4/29/24
to Google Ads API and AdWords API Forum
So I'm utilizing v23 of the googleads/google-ads-php.
I have all my credentials setup, and everything is working correctly.

I can confirm this by manually posting to the /token endpoint, and get an access token. However, utilizing the SDK I get "{ "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.", "code": 16, "status": "UNAUTHENTICATED", "details": [ { "@type": "type.googleapis.com\/google.ads.googleads.v16.errors.GoogleAdsFailure", "errors": [ { "errorCode": { "authenticationError": "OAUTH_TOKEN_HEADER_INVALID" }, "message": "Oauth token HTTP header is malformed." } ], "requestId": "k4fKAZ34qnUykl_MdjuIWw" } ] }"

It appears as though the access token is not being automatically generated.

Here is my code for getting the GoogleAdsClient object:

        public static function getGoogleAdsClient(): GoogleAdsClient
        {

            $oAuth2Credential = (new OAuth2TokenBuilder())
                ->withClientId(config('google-ads.client_id'))
                ->withClientSecret(config('google-ads.client_secret'))
                ->withRefreshToken(config('google-ads.refresh_token'))
                ->build();

            return (new GoogleAdsClientBuilder())
                ->withOAuth2Credential($oAuth2Credential)
                ->withDeveloperToken(config('google-ads.developer_token'))
                ->usingGapicV2Source(true)
                ->build();
        }

And then my quick test:

$client = App\Libraries\Google\Ads::getGoogleAdsClient();
       
        $googleAdsServiceClient = $client->getGoogleAdsServiceClient();

        $query = 'SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id';

        $stream = $googleAdsServiceClient->searchStream(
            SearchGoogleAdsStreamRequest::build(config('google-ads.customer_id'), $query)
        );

        foreach ($stream->iterateAllElements() as $googleAdsRow) {
            printf(
                "Campaign with ID %d and name '%s' was found.%s",
                $googleAdsRow->getCampaign()->getId(),
                $googleAdsRow->getCampaign()->getName(),
                PHP_EOL
            );
        }

Any help would be appreciated because I'm tearing my hair out trying to get this to work.

Google Ads API Forum Advisor

unread,
Apr 30, 2024, 7:15:06 AM4/30/24
to mike...@gmail.com, adwor...@googlegroups.com
Hi,

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

After verifying the provided logs, it is observed that you have encountered the "OAUTH_TOKEN_HEADER_INVALID " error when trying to make a request to the searchStream.

It appears that the credentials sent in your HTTP request are not valid and this is why you have encountered this error. The prevention tip recommended is to make sure you've passed the correct access token associated with your account. It's sometimes confused with refresh tokens and authorization codes. If you would like to get a credential that can access all client accounts under a manager account, make sure you get the refresh token for the manager account. For more details, see our guide on access token and refresh token and OAuth2

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

Thanks,
 
Google Logo Google Ads API Team


Reply all
Reply to author
Forward
0 new messages