Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get LinkedIn API access token without a redirect

I'm trying to use LinkedIn's API to access Universities LinkedIn pages to periodically collect how many followers they have. This seems doable, but I cant seem to generate an access token without having some weird redirect URL that has to take you to a GUI login page!

I'm using node.js for this, specifically this package: https://www.npmjs.org/package/node-linkedin

I have a API key and secret, so all I need is a access token then I'll be set to actually start using their API routes.

var Linkedin  = require('node-linkedin')('KEY', 'SECRET', 'callback');
var linkedin = Linkedin.init('my_access_token'); // need a token to initialise!

Any ideas?

Edit: Here's my code so far:

var Linkedin  = require('node-linkedin')('KEY', 'SECRET', './oauth/linkedin/callback');

app.get('/oauth/linkedin', function(req, res) {
  // This will ask for permisssions etc and redirect to callback url.
  Linkedin.auth.authorize(res, ['r_basicprofile', 'r_fullprofile', 'r_emailaddress', 'r_network', 'r_contactinfo', 'rw_nus', 'rw_groups', 'w_messages']);
});

app.get('/oauth/linkedin/callback', function(req, res) {
  Linkedin.auth.getAccessToken(res, req.query.code, function(err, results) {
    if ( err )
        return console.error(err);

    /**
     * Results have something like:
     * {"expires_in":5184000,"access_token":". . . ."}
     */

    console.log(results);
    var linkedin  = Linkedin.init(result);
    return res.redirect('/');
  });
});
like image 956
benharris Avatar asked Nov 18 '14 11:11

benharris


People also ask

What is redirect URI in LinkedIn?

The redirect URI will be used to define where to which URL the token will be sent to. The redirect URI is important because setting this up to a live site could let an attacker get access to your token. We will set this up to our local computer (or localhost).

What is authorized redirect urls for your app LinkedIn?

Linkedin's authorization page can be accessed by redirecting the user to https://www.linkedin.com/oauth/v2/authorization URL with the URL parameters listed below.


1 Answers

What you are trying to do is an application-only authentication, it seems linkedIn has removed this option unlike facebook and twitter. As from now it is only possible to authenticate as a user. If you really want to skip the redirect you could use something like PhantomJS which is a headerless browser. But i strongly recommend you not to do so as LinkedIn requires a user to authenticate in their license agreement. I don't know if it is legal but you could provide yourself an end-point which you use to generate the authentication_code and access_token and then save it to a database (60 days valid by default).

like image 87
Glenn Coessens Avatar answered Oct 10 '22 13:10

Glenn Coessens