How to make Linkedin Rest API request Node way (no JS SDK)?

I am using Node (v0.12.4) + Express (v4.0.0) + PassportJS (v0.1.17) to authenticate users with LinkedIn (OAuth2).

I am following the steps from this tutorial: https://developer.linkedin.com/docs/oauth2

I was able to authenticate the user and save their information, along with the Auth token provided by LinkedIn, to my database.

My question is , how do I access the API through a token? I am stuck in step 4 of this tutorial.

For example, how do I make the call below (taken from the tutorial above)?

Sample call

GET /v1/people/~ HTTP/1.1
Host: api.linkedin.com
Connection: Keep-Alive
Authorization: Bearer AQXdSP_W41_UPs5ioT_t8HESyODB4FqbkJ8LrV_5mff4gPODzOYR

      

Seems easy, but as a Node beginner I've struggled with it for too long.

==================

EDIT : Following Ted Avery's advice, I tried it request module

and got something like this:

// LinkedIn API route
app.get('/linkedin/people', function(req,res){
    request.get('http://api.linkedin.com/v1/people/~', {
        'host': 'api.linkedin.com',
        'connection': 'Keep-Alive'
        'auth': {
            'bearer': req.user.linkedin.token
        }
    }, function(error,apiRes,body){
        res.send(apiRes);
    });
});

      

And I get the following response ( apiRes

):

{
"statusCode":401

"body":"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<error>\n  <status>401</status>\n  <timestamp>1437750879622</timestamp>\n  <request-id>AKM2GX6BFE</request-id>\n  <error-code>0</error-code>\n  <message>ssl required</message>\n</error>\n",

"headers":{
    "server":"Apache-Coyote/1.1",
    "x-li-request-id":"AKM2GX6BFE",
    "date":"Fri, 24 Jul 2015 15:14:38 GMT",
    "vary":"*",
    "x-li-format":"xml",
    "content-type":"text/xml;charset=UTF-8",
    "content-length":"236",
    "x-li-fabric":"prod-ltx1",
    "x-li-pop":"prod-ltx1",
    "x-li-uuid":"zoW/s87q8xNQnsleUCsAAA==",
    "set-cookie":["lidc=\"b=TB60:g=105:u=27:i=1437750879:t=1437833236:s=AQFNZrhu0_0QIvH-rUkU4ElJ8Ytm_dKV\"; Expires=Sat, 25 Jul 2015 14:07:16 GMT; domain=.linkedin.com; Path=/"]
    },

"request":{
    "uri":{
        "protocol":"http:",
        "slashes":true,
        "auth":null,
        "host":"api.linkedin.com",
        "port":80,
        "hostname":"api.linkedin.com",
        "hash":null,
        "search":null,
        "query":null,
        "pathname":"/v1/people/~",
        "path":"/v1/people/~",
        "href":"http://api.linkedin.com/v1/people/~"
        },
    "method":"GET",
    "headers":{
        "authorization":"Bearer AQVYLfCs5lpbUlFdGeKXdR3z-3IiuO2N-PdJ7wgEtD_2doyxcy--mUxCN-GCJm-CaRXa-j7OF646enu_V5cp8jbiuMPesqKjWLcDdMmy8PSbEXS6Mw2iVznVF0Mk0iSAm419XlB7uMFwX0iAC71a_kjk_hZmvc90PmT471MLButnQmo3ww0"
        }
    }
}

      

I know the associated auth process is ok because I am getting username, email and token in my DB. The problem is that with this marker I always get 401 by default. Any thoughts on this?

+3


source to share


3 answers


Everything REST in Node is greatly simplified with the request library , so I recommend that. There are several examples of what you want in the docs. Sorry, I know a sample code would be the best, but it has been a while since I was working on a project with this library.



+1


source


The folder library you use to access LinkedIn may still ask for more default permissions scope permissions, which are now allowed (in line with the LinkedIn api changes that happened in February). I would recommend that you make sure the area requested when setting up a passport call is only limited to the member's basic permission r_basicprofile

to start and navigate from there.



IIRC, it usually requested r_fullprofile

the default, which is no longer available to the general public, which will result in an error when trying to authenticate under the new set of public permissions.

0


source


I know you asked this question a long time ago, but I have an answer and it might help others.

Replace " auth " with "Authorization"

// LinkedIn API route
app.get('/linkedin/people', function(req,res){
    request.get('http://api.linkedin.com/v1/people/~', {
        'host': 'api.linkedin.com',
        'connection': 'Keep-Alive'
        'Authorization': { // <------- Replace this
            'bearer': req.user.linkedin.token
        }
    }, function(error,apiRes,body){
        res.send(apiRes);
    });
});

      

Also read this: https://developer.linkedin.com/docs/oauth2#hero-par_longformtext_3_longform-text-content-par_resourceparagraph_3

You need to add as a parameter state=ThisIsRandomBlaBlaDCEeFWf45A53sdfKef424

when you make a GET request to https://www.linkedin.com/oauth/v2/authorization

0


source







All Articles