首页 文章

配置对Google My Business API的请求以避免出现未经身份验证的错误

提问于
浏览
0

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

为了它的 Value ,我正在使用请求承诺客户端来发出请求 . 我的功能如下 . 我刚收到访问令牌,所以我很确定它很好,我可以通过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);
  });
}

我的请求格式不正确还是我没有发送我需要的所有内容?

Update: 我遗漏了可能的关键信息,这是通过服务器端应用程序的一次性授权过程,如下所述:https://developers.google.com/identity/protocols/OAuth2 . 我的麻烦被包含在该页面的句子中,"After an application obtains an access token, it sends the token to a Google API in an HTTP authorization header."

1 回答

  • 0

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

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

相关问题