Google Ads API PHP 400 错误请求“错误”:“invalid_grant”

时间:2021-06-11 11:51:59

标签: php google-ads-api

我正在尝试使用谷歌广告 PHP SDK 连接到谷歌广告 API。我按照说明操作,并成功启动了 GoogleAdsServiceClient,但是当我尝试使用搜索方法时,它崩溃了,只说“invalid_grant”,而没有关于如何从错误中恢复的说明。有没有人知道如何调试或解决这个问题?

googleads/google-ads-php

我收到的错误消息。

Client error: `POST https://oauth2.googleapis.com/token` resulted in a `400 Bad Request` response:
{
    "error": "invalid_grant",
    "error_description": "Bad Request"
}

错误发生在

/vendor/googleads/google-ads-php/src/Google/Ads/GoogleAds/V7/Services/Gapic/GoogleAdsServiceGapicClient.php -> getPagedListResponse(第 457 行)

return $this->getPagedListResponse('Search', $optionalArgs, SearchGoogleAdsResponse::class, $request);

我美丽的代码

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

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

$serviceClient = $api->getGoogleAdsServiceClient();
$query = 'SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id';

//this is were it crashes and returns 400
$stream = $serviceClient->search($this->customerId, $query);

foreach ($stream->iterateAllElements() as $googleAdsRow) {
    dd($googleAdsRow);
}


0 个答案:

没有答案