“Request had invalid authentication credentials. Expected OAuth 2 access token…” Error

huangapple go评论63阅读模式
英文:

Google Ads API, PHP: "Request had invalid authentication credentials. Expected OAuth 2 access token..." Error

问题

我尝试使用以下PHP代码来进行Google Ads API的cURL请求:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://googleads.googleapis.com/v14/customers/[CUSTOMER_ID]:generateKeywordHistoricalMetrics');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'developer-token: [DEVELOPER_TOKEN]',
    'login-customer-id: [LOGIN_CUSTOMER_ID]',
    'Authorization: Bearer [???????]',
    'Accept: application/json',
    'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"historicalMetricsOptions":{"yearMonthRange":{"start":{"month":"JUNE","year":2022},"end":{"month":"MAY","year":2023}}},"keywords":[""]}');
$response = curl_exec($ch);
curl_close($ch);

而响应是:

{
  "error": {
    "code": 401,
    "message": "请求具有无效的身份验证凭据。期望OAuth 2访问令牌、登录Cookie或其他有效的身份验证凭据。请参阅https://developers.google.com/identity/sign-in/web/devconsole-project。",
    "status": "UNAUTHENTICATED"
  }
}

我认为问题可能出在"Authentication"字段上。到目前为止,我在该字段中尝试了以下内容(每次都失败了):

  1. OAuth 2客户端ID
  2. OAuth 2客户端秘密
  3. OAuth 2刷新令牌,适用于我创建的另一个Google Ads项目
  4. 开发者令牌

因此,我会感激任何和所有的帮助,因为我正在耗尽主意,不知道该怎么办。

英文:

I'm attempting to cURL the Google Ads API with the following PHP code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://googleads.googleapis.com/v14/customers/[CUSTOMER_ID]:generateKeywordHistoricalMetrics');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'developer-token: [DEVELOPER_TOKEN',
    'login-customer-id: [LOGIN_CUSTOMER_ID]',
    'Authorization: Bearer [???????]',
    'Accept: application/json',
    'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"historicalMetricsOptions":{"yearMonthRange":{"start":{"month":"JUNE","year":2022},"end":{"month":"MAY","year":2023}}},"keywords":[""]}');

$response = curl_exec($ch);

curl_close($ch);

And the response is:

{
  "error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
  }
}

What I believe the issue to be is on the "Authentication" field. So far, I've put the following in that field(and failed each time):

  1. OAUTH 2 Client ID
  2. OAUTH 2 Client Secret
  3. OAUTH 2 Refresh Token, that works for another Google Ads project I've made
  4. Developer token

So, I could appreciate any and all help, since I'm running out of ideas and don't know what to do.

答案1

得分: 0

你需要使用长期的OAuth2刷新令牌来获取短期的访问令牌,然后将其用于Authorization头部。

当你使用客户端库时,这个交换过程会自动处理,但也可以通过手动的HTTP请求来完成,就像下面这样:

POST /token HTTP/1.1
Host: oauth2.googleapis.com
Content-Type: application/x-www-form-urlencoded

client_id=<OAUTH2_CLIENT_ID>&
client_secret=<OAUTH2_CLIENT_SECRET>&
refresh_token=<OAUTH2_REFRESH_TOKEN>&
grant_type=refresh_token
英文:

You'll need to exchange your long-lived OAuth2 refresh token for an short-lived access token and use that for the Authorization header.

The exchange is handled automatically for you when you use a client library, but can also be done with a manual HTTP request, like such:

POST /token HTTP/1.1
Host: oauth2.googleapis.com
Content-Type: application/x-www-form-urlencoded

client_id=&lt;OAUTH2_CLIENT_ID&gt;&amp;
client_secret=&lt;OAUTH2_CLIENT_SECRET&gt;&amp;
refresh_token=&lt;OAUTH2_REFRESH_TOKEN&gt;&amp;
grant_type=refresh_token

huangapple
  • 本文由 发表于 2023年6月19日 19:40:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506268.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定