【问题标题】:How to connect to a third party API from node.js using Express.js如何使用 Express.js 从 node.js 连接到第三方 API
【发布时间】:2017-11-14 16:56:54
【问题描述】:

我正在尝试使用授权密钥连接到第三方 api,但没有成功。任何建议/方向/评论将不胜感激。

server.js

...

app.get('/products', (req, res) => {

request.get(BASE_URL, {
  'auth': {
  'bearer': 'abcdsfegete'
}
 }).on('error', function (error) {
    console.log(error);

 }).on('response', function (response) {
    console.log(response);
 })
res.send();
})
...

结果:我不断收到“此响应必须有可用数据”。

【问题讨论】:

    标签: node.js rest express


    【解决方案1】:

    问题似乎是属性错误(authbearer)。因此,改为使用基本身份验证。

    app.get('/products', (req, res) => {
    const options = {
    url: BASE_URL + '/products',
    headers: {
      'Authorization': 'abcdefghghgh',
      'Accept': 'application/json'
    }
    };
       request.get(options).pipe(res);
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-16
      • 2012-04-16
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多