Google Adwords API: INVALID_TOKEN_HEADER when using new API and correct Developer Token (i believe)
I am having an issue with Google Adwords API and can't seem to get it to work.
I have taken the following steps:
- Installed composer
- Installed Google Adwords API
- Created a Developer Token
- Created Oauth2
- Created a Refresh Token
- Updated all of the above inside of adsapi_php which is in the root
- I also have Basic access to the API
<?php namespace Google\AdsApi\Examples\AdWords\v201809\Reporting; require __DIR__ . '/vendor/autoload.php'; use Google\AdsApi\AdWords\AdWordsServices; use Google\AdsApi\AdWords\AdWordsSession; use Google\AdsApi\AdWords\AdWordsSessionBuilder; use Google\AdsApi\AdWords\v201809\cm\ReportDefinitionReportType; use Google\AdsApi\AdWords\v201809\cm\ReportDefinitionService; use Google\AdsApi\Common\OAuth2TokenBuilder; /** * This example gets the fields available in a campaign report. */ class GetReportFields{ const PAGE_LIMIT = 500; public static function runExample(AdWordsServices $adWordsServices, AdWordsSession $session){ $reportDefinitionService = $adWordsServices->get($session, ReportDefinitionService::class); // The type of the report to get fields for. $reportType = ReportDefinitionReportType::CAMPAIGN_PERFORMANCE_REPORT; // Get report fields of the report type. $reportDefinitionFields = $reportDefinitionService->getReportFields($reportType); printf( "The report type '%s' contains the following fields:\n", $reportType ); foreach ($reportDefinitionFields as $reportDefinitionField) { printf( ' %s (%s)', $reportDefinitionField->getFieldName(), $reportDefinitionField->getFieldType() ); if ($reportDefinitionField->getEnumValues() !== null) { printf( ' := [%s]', implode(', ', $reportDefinitionField->getEnumValues()) ); } print "\n"; } } public static function main(){ // Generate a refreshable OAuth2 credential for authentication. $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build(); // Construct an API session configured from a properties file and the // OAuth2 credentials above. $session = (new AdWordsSessionBuilder())->fromFile()->withOAuth2Credential($oAuth2Credential)->build(); self::runExample(new AdWordsServices(), $session); } } GetReportFields::main(); ?>
But I am receiving the following error:
Fatal error: Uncaught Google\AdsApi\AdWords\v201809\cm\ApiException: [QuotaCheckError.INVALID_TOKEN_HEADER @ ; trigger:'New developers must use the Google Ads API: https://developers.google.com/google-ads/api/docs/start'] in C:\xampp\htdocs\vendor\googleads\googleads-php-lib\src\Google\AdsApi\Common\Util\Reflection.php:43 Stack trace: #0 C:\xampp\htdocs\vendor\googleads\googleads-php-lib\src\Google\AdsApi\Common\Util\Reflection.php(43): ReflectionClass->newInstanceArgs(Array) #1 C:\xampp\htdocs\vendor\googleads\googleads-php-lib\src\Google\AdsApi\Common\AdsSoapClient.php(203): Google\AdsApi\Common\Util\Reflection->createInstance('Google\\AdsApi\\A...', Array) #2 C:\xampp\htdocs\vendor\googleads\googleads-php-lib\src\Google\AdsApi\Common\AdsSoapClient.php(165): Google\AdsApi\Common\AdsSoapClient->parseApiExceptionFromSoapFault(Object(SoapFault)) #3 C:\xampp\htdocs\vendor\googleads\googleads-php-lib\src\Google\AdsApi\AdWords\v201809\cm\ReportDefinitionService.php(89): Google\AdsApi\Common\AdsSoapClient->__soapCall('getReportFields', Array) #4 C:\xampp\htdocs\adwords.php(31): Google\AdsApi\AdWords\v201809\cm\ReportDefinitionService->getReportFields('CAMPAIGN_PERFOR...') #5 C:\xampp\htdocs\adwords.php(61): Google\AdsApi\Examples\AdWords\v201809\Reporting\GetReportFields::runExample(Object(Google\AdsApi\AdWords\AdWordsServices), Object(Google\AdsApi\AdWords\AdWordsSession)) #6 C:\xampp\htdocs\adwords.php(65): Google\AdsApi\Examples\AdWords\v201809\Reporting\GetReportFields::main() #7 {main} thrown in C:\xampp\htdocs\vendor\googleads\googleads-php-lib\src\Google\AdsApi\Common\Util\Reflection.php on line 43
Now I am certain the credentials are all correct. Inside of https://developers.google.com/adwords/api/docs/common-errors#QuotaCheckError.INVALID_TOKEN_HEADER it states there are only two potential issues:
- The developer token is not set on the request or it contains a typo.
- The namespace of the headers in the request is not correct.
So the only issue could be the namespace of the headers but I do not understand how to: "Ensure that the headers of the request are in the namespace https://adwords.google.com/api/adwords/cm/{version}, where {version} is replaced with the version of the API being used."
Where do I find the version to confirm if this is the issue or if it is somehow that I am using the wrong API which a new developer tokens are unable to be used on: https://developers.google.com/adwords/api/docs/guides/signup? Although I did follow the new documentation to the letter...
Or is it that it is unable to find the adsapi_php? Thus it is always wrong? Any advice for finding out where the issues lies? I have been trying to troubleshoot this since 10am this morning.
If it helps here is my composer.json:
{ "name": "phpmyadmin/phpmyadmin", "type": "project", "description": "A web interface for MySQL and MariaDB", "keywords": ["phpmyadmin","mysql","web"], "homepage": "https://www.phpmyadmin.net/", "support": { "forum": "https://www.phpmyadmin.net/support/", "issues": "https://github.com/phpmyadmin/phpmyadmin/issues", "wiki": "https://wiki.phpmyadmin.net/", "docs": "https://docs.phpmyadmin.net/", "source": "https://github.com/phpmyadmin/phpmyadmin" }, "license": "GPL-2.0-only", "authors": [ { "name": "The phpMyAdmin Team", "email": "developers@phpmyadmin.net", "homepage": "https://www.phpmyadmin.net/team/" } ], "non-feature-branches": ["RELEASE_.*"], "autoload": { "psr-4": { "PhpMyAdmin\\": "libraries/classes" } }, "autoload-dev": { "psr-4": { "PhpMyAdmin\\Tests\\": "test/classes", "PhpMyAdmin\\Tests\\Selenium\\": "test/selenium/" } }, "repositories": [ { "type": "composer", "url": "https://www.phpmyadmin.net" } ], "require": { "php": "^7.1.3 || ^8.0", "ext-hash": "*", "ext-iconv": "*", "ext-json": "*", "ext-mysqli": "*", "ext-pcre": "*", "ext-xml": "*", "firebase/php-jwt": "^3.0", "google/apiclient": "^2.0", "google/gax": "^1.7", "google/recaptcha": "^1.1", "googleads/google-ads-php": "^10.1", "googleads/googleads-php-lib": "^53.0", "guzzlehttp/guzzle": "^7.0", "nikic/fast-route": "^1.3", "phpmyadmin/motranslator": "^5.0", "phpmyadmin/shapefile": "^2.0", "phpmyadmin/sql-parser": "^5.0", "phpmyadmin/twig-i18n-extension": "^3.0", "phpseclib/phpseclib": "^2.0", "react/http": "^1.4", "spatie/crawler": "^7.0", "symfony/config": "^4.4.9", "symfony/dependency-injection": "^4.4.9", "symfony/expression-language": "^4.4.9", "symfony/polyfill-ctype": "^1.17.0", "symfony/polyfill-mbstring": "^1.17.0", "twig/twig": "^2.9 || ^3", "williamdes/mariadb-mysql-kbs": "^1.2" }, "conflict": { "phpseclib/phpseclib": "2.0.8", "tecnickcom/tcpdf": "<6.2", "pragmarx/google2fa": "<6.1.0 || >8.0", "pragmarx/google2fa-qrcode": "<1.0.1", "samyoul/u2f-php-server": "<1.1" }, "suggest": { "ext-openssl": "Cookie encryption", "ext-curl": "Updates checking", "ext-opcache": "Better performance", "ext-zlib": "For gz import and export", "ext-bz2": "For bzip2 import and export", "ext-zip": "For zip import and export", "ext-gd2": "For image transformations", "ext-mbstring": "For best performance", "tecnickcom/tcpdf": "For PDF support", "pragmarx/google2fa-qrcode": "For 2FA authentication", "samyoul/u2f-php-server": "For FIDO U2F authentication" }, "require-dev": { "php-webdriver/webdriver": "^1.11", "phpmyadmin/coding-standard": "^2.1.1", "phpstan/extension-installer": "^1.1", "phpstan/phpstan": "^0.12.79", "phpstan/phpstan-phpunit": "^0.12.17", "phpunit/phpunit": "^7.5 || ^8.0 || ^9.0", "pragmarx/google2fa-qrcode": "^1.0.1", "samyoul/u2f-php-server": "^1.1", "symfony/console": "^4.4", "symfony/finder": "^4.4", "symfony/twig-bridge": "^4.4", "tecnickcom/tcpdf": "^6.4.1", "vimeo/psalm": "^4.7.0", "google/apiclient": "^2.0", "guzzlehttp/guzzle": "^7.0", "firebase/php-jwt": "^3.0" }, "extra": { "branch-alias": { "dev-master": "5.2.x-dev" } }, "scripts": { "phpcbf": "phpcbf", "phpcs": "phpcs", "phpstan": "phpstan analyse", "psalm": "psalm", "phpunit": "phpunit --color=always", "test": [ "@phpcs", "@phpstan", "@psalm", "@phpunit" ], "update:baselines": "phpstan analyse --generate-baseline && psalm --set-baseline=psalm-baseline.xml", "twig-lint": "php scripts/console lint:twig templates --ansi --show-deprecations" }, "config":{ "sort-packages": true, "discard-changes": true } }
The answer is right there in the error message :)
You're a new developer, but you're using the old API. The new API is located here: https://developers.google.com/google-ads/api/docs/start
The official PHP library for this API is located here: https://github.com/googleads/google-ads-php/