英文:
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"字段上。到目前为止,我在该字段中尝试了以下内容(每次都失败了):
- OAuth 2客户端ID
- OAuth 2客户端秘密
- OAuth 2刷新令牌,适用于我创建的另一个Google Ads项目
- 开发者令牌
因此,我会感激任何和所有的帮助,因为我正在耗尽主意,不知道该怎么办。
英文:
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):
- OAUTH 2 Client ID
- OAUTH 2 Client Secret
- OAUTH 2 Refresh Token, that works for another Google Ads project I've made
- 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=<OAUTH2_CLIENT_ID>&
client_secret=<OAUTH2_CLIENT_SECRET>&
refresh_token=<OAUTH2_REFRESH_TOKEN>&
grant_type=refresh_token
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论