using Linkedin v2 API to login and get profile, company pages and more


| 0 Comments

rest-api node express javascript

recently worked on a node js project which requires to login via Linkedin API and then use access token generated via login to access profile, company pages data. here are the steps i followed to make it work.

Create App on LinkedIn

go to Developers page and create an app.

after creating an app, go to app Auth settings, copy client id and client secret.

add Redirect URL.

Login using Linkedin OAuth2

install Passport

npm install passport --save

install Passport Linkedin Strategy

npm install passport-linkedin-oauth2

in your index.js (or main file)

index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// paspport dependencies

var passport = require('passport');
var LinkedInStrategy = require('passport-linkedin-oauth2').Strategy;

// linkedin app settings
var LINKEDIN_CLIENT_ID = "CLIENT_ID_HERE";
var LINKEDIN_CLIENT_SECRET = "CLIENT_SECRET_HERE";
var Linkedin = require('node-linkedin')(LINKEDIN_CLIENT_ID, LINKEDIN_CLIENT_SECRET);

passport.serializeUser(function (user, done) {
    done(null, user);
});

passport.deserializeUser(function (obj, done) {
    done(null, obj);
});

passport.use(new LinkedInStrategy({
    clientID: LINKEDIN_CLIENT_ID,
    clientSecret: LINKEDIN_CLIENT_SECRET,
    callbackURL: "http://127.0.0.1:3000/auth/linkedin/callback",
    scope: ['r_emailaddress', 'r_basicprofile', 'rw_company_admin'],
    passReqToCallback: true
},
function (req, accessToken, refreshToken, profile, done) {
	req.session.accessToken = accessToken;
    process.nextTick(function () {
        return done(null, profile);
	});
}));

// for auth

app.get('/auth/linkedin',
  passport.authenticate('linkedin', { state: 'SOME STATE'  }),
  function(req, res){
    // The request will be redirected to LinkedIn for authentication, so this
    // function will not be called.
});

// for callback

app.get('/auth/linkedin/callback', passport.authenticate('linkedin', { failureRedirect: '/' }),
function (req, res) {
    res.redirect('/');
});



now you have access token saved in req.session.accesstoken variable.

Using Access Token to make REST API Calls for data

install node-linkedin package from npm

npm install node-linkedin --save

get all company pages (where logged in user is admin)

index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
app.get('/companies', function (req, res) {

    var user_companies = null;
    if (req.session.accessToken != undefined) {
        var linkedin = Linkedin.init(req.session.accessToken);
        linkedin.companies.asAdmin(function (err, companies) {
            this.user_companies = companies;
            
            res.json(this.user_companies);
            // now use this data in view
            // e.g.
            // res.render('index', { companies: user_companies });
        });
    }
    else
    {
        res.render('index', { companies: user_companies });
    }
});



get company info by id

index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
app.get('/company/:id', function(req,res){
    if (req.session.accessToken != undefined) {
        var linkedin = Linkedin.init(req.session.accessToken);
        linkedin.companies.company(req.params.id, function (err, company) {
            res.json(company);
            // res.render('company', { company: company });
        });
    }
    else
    {
        res.send('not found');
    }
})



i hope this helps you in creating an linked in app and using it to access data via REST API calls.

let me know in the comments if you need some help.