配置对"Google 我的商家"API 的请求,以避免出现未经身份验证的错误



在我的 Node.js 应用中向 Google My Business 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"
}
}

对于它的价值,我正在使用请求-承诺客户端来发出请求。我的函数如下。我刚刚收到访问令牌,所以我相当确定它是好的,我可以通过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。我的麻烦包含在该页面上的句子中,"应用程序获得访问令牌后,它会将令牌发送到HTTP授权标头中的Google API。

原来我的标题格式不正确。授权密钥应位于headers对象中:

{
method: 'GET',
uri: `https://mybusiness.googleapis.com/v4/accounts/${ACCOUNT_ID}/locations/${loc._id}`,
headers: {
Authorization: `OAuth ${ACCESS_TOKEN}`
}
}

相关内容

最新更新