在我的 Node.js 应用中向 Google 我的商家 API 提交请求时,我不断收到未经身份验证的错误。响应:
{
"error": {
"code": 401,
"message": "Request is missing required authentication credential. 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"
}
}
无论如何,我使用请求 promise 客户端来发出请求。我的功能如下。我刚刚收到访问 token ,所以我相当确定它是好的,并且我可以通过 err.options.Authorization 日志看到它。我知道位置 ID 尚不存在,但我不认为这就是错误告诉我的内容。
const request = require('request-promise');
...
function checkLocation (loc) {
return request({
method: 'GET',
uri: `https://mybusiness.googleapis.com/v4/accounts/${ACCOUNT_ID}/locations/${loc._id}`,
Authorization: `OAuth ${ACCESS_TOKEN}`
})
.then(function (response) {
console.log(response);
})
.catch(function (err) {
console.error(err.error, err.options.Authorization);
});
}
我的请求格式是否不正确,或者我没有发送所需的全部内容?
更新:我遗漏了可能重要的信息,即这是通过服务器端应用程序的一次性授权过程进行的,如下所述:https://developers.google.com/identity/protocols/OAuth2 。我的麻烦就在该页面上的一句话中:“应用程序获取访问 token 后,它会在 HTTP 授权 header 中将该 token 发送到 Google API。”
最佳答案
结果我的标题格式不正确。授权 key 应位于 headers
对象中:
{
method: 'GET',
uri: `https://mybusiness.googleapis.com/v4/accounts/${ACCOUNT_ID}/locations/${loc._id}`,
headers: {
Authorization: `OAuth ${ACCESS_TOKEN}`
}
}
关于node.js - 配置对 Google 我的商家 API 的请求以避免未经身份验证的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50125117/