Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinkedIn oauth error A required parameter "client_id" is missing

so I am just upgrading from LinkedIn oauth 1.0 to 2.0 and I have been getting this error for about a day now. I saw a post about it for php but I cannot figure it out in Node JS (Javascript) here is my current code :

axios
    .post("https://www.linkedin.com/oauth/v2/accessToken", {
      grant_type: "authorization_code",
      code: req.query.code,
      redirect_uri: keys.linkedinCallbackURL,
      client_id: keys.linkedinConsumerKey,
      client_secret: keys.linkedinConsumerSecret
    })
    .then(res2 => {
      console.log(res2);
    })
    .catch(error => {
      console.log(error);
    });

If you have any ideas let me know :)

Link to php solution: LinkedIn OAuth a required parameter "clien_id" is missing

Link to LinkedIn guide: https://developer.linkedin.com/docs/oauth2 (error occurring on step 3)

like image 666
peter Avatar asked Dec 17 '22 20:12

peter


1 Answers

The LinkedIn documentation ask you to send the data as a application/x-www-form-urlencoded and they show us this exemple:

POST /oauth/v2/accessToken HTTP/1.1
Host: www.linkedin.com
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=987654321&redirect_uri=https%3A%2F%2Fwww.myapp.com%2Fauth%2Flinkedin&client_id=123456789&client_secret=shhdonottell

The Axios documentation says that by default the body is serialized as JSON:

By default, axios serializes JavaScript objects to JSON.

In order to serialize the body properly you should use the querystring module as following:

const querystring = require('querystring');

axios
    .post("https://www.linkedin.com/oauth/v2/accessToken", querystring.stringify({
      grant_type: "authorization_code",
      code: req.query.code,
      redirect_uri: keys.linkedinCallbackURL,
      client_id: keys.linkedinConsumerKey,
      client_secret: keys.linkedinConsumerSecret
    }));
like image 194
Yannick Loriot Avatar answered Dec 28 '22 11:12

Yannick Loriot