Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get user profile data after getting access token from LinkedIn via Oath2

I have a small node application that is retrieving an access token from LinkedIn after authenticating (authorizing) with Oath2.

Now that I have the token I want to make a subsequent request to get a users profile data. The problem is that when I make the request the error says the following:

{"success":false,"msg":"403 - {\"serviceErrorCode\":100,\"message\":\"Not enough permissions to access: GET /me\",\"status\":403}"}

I've checked off all the "state" options in the LinkedIn app.

enter image description here

According to the LinkedIn API docs the token needs to be sent in a header. Like this:

enter image description here

( reference: https://developer.linkedin.com/docs/oauth2#requests )

The route to get profile data is listed as: https://api.linkedin.com/v2/me

enter image description here

Here is the route that I want users to hit to get their profile data:

app.get("/user",async (req,res)=>{

    console.log(token); /* token object is available in higher scope from previous request */

     var options = {

        url: "https://api.linkedin.com/v2/me",
        method: 'GET',
        headers: {
          'Host': "api.linkedin.com",
          'Connection': "Keep-Alive",
          'Authorization': 'Bearer ' + token.access_token
        },

        json: true // Automatically stringifies the body to JSON
    };




    try {
        var response = await makeRequest(options);
        return res.json({success:true})

        } catch (err) {

        return res.json({ success: false, msg: err.message});; // TypeError: failed to fetch
    }



});
like image 899
William Avatar asked Nov 06 '22 23:11

William


1 Answers

Try adding a space after the word Bearer. Currently it sees your token and the word Bearer as a single string and it doesn't know where to split to get the token

'Authorization': 'Bearer ' + token.access_token

like image 145
JacobW Avatar answered Nov 14 '22 09:11

JacobW