1

在我的 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"
  }
}

对于它的价值,我正在使用 request-promise 客户端来发出请求。我的功能如下。我刚刚收到访问令牌,所以我相当肯定它是好的,我可以通过 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。”

4

1 回答 1

2

原来我的标题格式不正确。授权密钥应该在一个headers对象中:

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

    Authorization: `OAuth ${ACCESS_TOKEN}`

  }
}
于 2018-05-03T20:46:14.910 回答