php - Google Adwords API:INVALID_TOKEN_HEADER 使用新 API 和正确的开发者令牌时(我相信)
问题描述
我在使用 Google Adwords API 时遇到问题,似乎无法让它工作。
我已采取以下步骤:
- 安装作曲家
- 已安装 Google Adwords API
- 创建了一个开发者令牌
- 创建 Oauth2
- 创建了一个刷新令牌
- 更新了根目录中 adsapi_php 中的所有上述内容
- 我还拥有 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();
?>
但我收到以下错误:
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
现在我确定凭据都是正确的。在https://developers.google.com/adwords/api/docs/common-errors#QuotaCheckError.INVALID_TOKEN_HEADER内部,它指出只有两个潜在问题:
- 开发人员令牌未在请求中设置或包含拼写错误。
- 请求中标头的命名空间不正确。
所以唯一的问题可能是标头的名称空间,但我不明白如何:“确保请求的标头位于名称空间https://adwords.google.com/api/adwords/cm/{version},其中 {version} 替换为正在使用的 API 版本。"
我在哪里可以找到版本来确认这是否是问题所在,或者我是否使用了错误的 API,无法使用新的开发人员令牌:https ://developers.google.com/adwords/api /文档/指南/注册?尽管我确实遵循了新文档的信...
还是找不到adsapi_php?所以总是错的?有什么建议可以找出问题所在吗?从今天早上 10 点开始,我一直在尝试解决此问题。
如果它有帮助是我的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
}
}
解决方案
答案就在错误消息中:)
您是新开发人员,但您使用的是旧 API。新 API 位于此处:https ://developers.google.com/google-ads/api/docs/start
此 API 的官方 PHP 库位于此处: https ://github.com/googleads/google-ads-php/
推荐阅读
- python - 无法加载需要“wx”交互式框架的后端“WXAgg”,因为“无头”当前正在运行
- r - 当您在同一台机器上有多个 R 版本时如何更改默认库路径
- c# - 将最新的 chrome 引擎与您的 UWP 构建打包在一起,以避免 PWA 的 Web 视图中的旧 Internet Explorer 问题
- r - 删除缺少数据的行。如何从数据框中省略任一列中缺少值的行
- python - 保存修改后的 xml 错误“元素树”对象没有属性“标签”
- oracle - 从 JSON_OBJECT 输出中删除转义字符
- tensorflow - 神经网络框架和 RL 算法库有什么区别?
- blazor - 如何将身份验证令牌传递给多个 http 客户端?
- excel - 我可以让 Excel 通过查找并从谷歌表格中获取数据吗?
- functional-programming - type1 -> type2 -> type3 和 type1 -> (type2 -> type3) 有什么区别?