1

I have a problem with my api call using NodeJS. I have no problem with postman, but when I run it with Node, server response is 401.

Here's the nodejs code:

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);
});

So basically here's my (perfectly working) Postman request.

Any idea what's wrong with the syntax or how to properly implement the oauth signature in the headers ?

jlewkovich
  • 2,725
  • 2
  • 35
  • 49
  • Do you get a different result using request's `oauth` [option](https://github.com/request/request#oauth-signing) which builds the oauth parameter strings for you? – varontron Jan 16 '20 at 16:07

1 Answers1

0

HTTP 401 error means "unauthorized". I don't think OAuth authorization is meant to be passed through the headers.

It is supposed to be something like :

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)
})

Check the request docs for more explanation on how to deal with OAuth in request.

(I strongly suggest using Axios instead of request, but that's up to you)

rom1bl
  • 133
  • 2
  • 6
  • Is there a way to do this in axios ? – Ryan Aquino Dec 07 '20 at 03:16
  • Yes. But this is OAuth 1, and many APIs are using OAuth 2 today which is not the exact same process of authentication. For syntax examples, you could check out [axios docs](https://github.com/axios/axios) or [this post](https://stackoverflow.com/questions/41519092/using-axios-get-with-authorization-header-in-react-native-app) – rom1bl Dec 08 '20 at 11:40
  • I need OAuth 1 for Jira OAuth. I have posted a question regarding this : https://stackoverflow.com/questions/65193587/how-to-create-oauthv1-rsa-sha1-postman-authorization-request-header-with-axios. Maybe you can take a look if you know th solution. – Ryan Aquino Dec 08 '20 at 12:02