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 ?

1
  • Do you get a different result using request's oauth option which builds the oauth parameter strings for you? Commented Jan 16, 2020 at 16:07

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to do this in axios ?
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 or this post
I need OAuth 1 for Jira OAuth. I have posted a question regarding this : stackoverflow.com/questions/65193587/…. Maybe you can take a look if you know th solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.