node.js - 配置对 Google 我的商家 API 的请求以避免未经身份验证的错误

标签 node.js oauth-2.0 node-request google-my-business-api

在我的 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/

相关文章:

spring - 如何在 Spring Security JDBC 中保留 oauth 访问 token ?

node.js - 使用 NodeJS + Request 模块时图像无法正确代理

javascript - async.each 不运行回调函数

node.js - 无法从主机访问副本集mongodb(docker)

oauth-2.0 - oAuth2.0 : Why need "authorization-code" and only then the token?

azure - 如何为下游应用程序设置Azure AD应用程序权限?

node.js - 如何使用node.js httpRequest连接到dashdb

node.js - Knex 从时间列和日期列中获取格式化时间

node.js - 通过 RabbitMQ 发送数组

node.js - 唯一标识HTTPS证书