【问题标题】:API Call with NodeJS signing with Oauth1 not working使用 Oauth1 签名的 NodeJS 的 API 调用不起作用
【发布时间】:2020-05-03 12:11:31
【问题描述】:

我在使用 NodeJS 的 api 调用时遇到问题。我用 postman 没问题,但是当我用 Node 运行它时,服务器响应是 401。

这是 nodejs 代码:

var request = require("request");

var options = { method: 'GET',
  url: 'https://api.cardmarket.com/ws/v2.0/output.json/products/find',
  qs: 
   { search: 'Salamangrande%25Loup%25Du%25Soleil',
     idGame: '3',
     idLanguage: '2' },
  headers: 
   { 'cache-control': 'no-cache',
     Connection: 'keep-alive',
     Cookie: 'PHPSESSID=m399v2el9635i3jq0e4did8f5k',
     'Accept-Encoding': 'gzip, deflate',
     Host: 'api.cardmarket.com',
     'Postman-Token': '9b19a85d-8888-4f31-8d24-fe309a55bf76,4d8f4741-4b85-4d5e-85dc-8ac43ea5f56c',
     'Cache-Control': 'no-cache',
     Accept: '*/*',
     'User-Agent': 'PostmanRuntime/7.19.0',
     Authorization: 'OAuth realm="https%3A%2F%2Fapi.cardmarket.com%2Fws%2Fv2.0%2Foutput.json%2Fproducts%2Ffind",oauth_consumer_key="xxxxxxxxx",oauth_token="xxxxxxxxx",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1579186959",oauth_nonce="vWUKBoTTkle",oauth_version="1.0",oauth_signature="ZD2TwzLVHqOjLm3u%2BWv%2FsQ8mdfs%3D"' } };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(response.statusCode);
});

所以基本上这是我的(完美工作)。

知道语法有什么问题或如何在标头中正确实现 oauth 签名吗?

【问题讨论】:

  • 您使用请求的oauth option 为您构建oauth 参数字符串是否得到不同的结果?

标签: node.js api oauth get httprequest


【解决方案1】:

HTTP 401 错误意味着“未经授权”。 我不认为 OAuth 授权是要通过标头传递的。

应该是这样的:

var request = require("request");

url='https://api.cardmarket.com/ws/v2.0/output.json/products/find';

oauth = {
     consumer_key: CONSUMER_KEY,
     consumer_secret: CONSUMER_SECRET,
     token: AUTH_TOKEN,
     token_secret: TOKEN_SECRET,
     signature_method : 'RSA-SHA1',

};

qs = {
     search: 'Salamangrande%25Loup%25Du%25Soleil',
     idGame: '3',
     idLanguage: '2'
};

request.get({url:url, oauth:oauth, qs:qs, json:true}, function (e, r, product) {
  console.log(product)
})

查看request docs 以获取有关如何处理请求中的 OAuth 的更多说明。

(我强烈建议使用 Axios 而不是 request,但这取决于你)

【讨论】:

  • 有没有办法在 axios 中做到这一点?
  • 是的。但这是 OAuth 1,现在许多 API 都在使用 OAuth 2,这与身份验证过程并不完全相同。对于语法示例,您可以查看 axios docsthis post
  • 我需要用于 Jira OAuth 的 OAuth 1。我已经发布了一个关于此的问题:stackoverflow.com/questions/65193587/…。如果您知道解决方案,也许您可​​以看看。
猜你喜欢
  • 2019-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-11
  • 1970-01-01
  • 2018-10-10
相关资源
最近更新 更多