How to get LinkedIn API access token without a redirect
Asked Answered
M

1

7

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('/');
  });
});
Muimuir answered 18/11, 2014 at 11:21 Comment(14)
you're not authenticating.. github.com/ArkeologeN/node-linkedin#oauth-20Blomquist
so i need to use this route? /oauth/linkedin/callbackMuimuir
I get this error when I go there {"error_description":"missing required parameters, includes an invalid parameter value, parameter more than once. : Unable to retrieve access token : authorization code not found","error":"invalid_request"}Muimuir
can you post code of your auth ? with your callback ? credentials cleared ofcBlomquist
are you using relative directory in the callback url for any particular reason ?Blomquist
only because I'm on localhost at the moment and don't want to swap it over to the live url when it's done, I get this with the whole url: i.imgur.com/5jjvbZZ.jpg Seems impossible to dev for, as it wants the url of the live app!Muimuir
i mean on your require constructor, you're using a relative callbacl, './oauth/linkedin/callback' im sure its no problem.. but just incaseBlomquist
yeah I changed it to http://localhost:3000/oauth/linkedin/callback and got this error: i.imgur.com/5jjvbZZ.jpgMuimuir
looks like you need to add the URL to your KEY : developer.linkedin.com/forum/…Blomquist
i.imgur.com/OragoZN.png This is what I'm trying to avoid, I dont want this annoying GUI login page, I just want it to back end authenticate, as this script is supposed to run automatically on its own monthlyMuimuir
@Blomquist thanks for your help man, but I think it's impossible, man this API sucksMuimuir
It has to be possible, but i agree with you, i don't enjoy Oath, as a user of it or developer for it!Blomquist
Did you come up with any solution without having to manually grant access every 60 days with the GUI?Toluol
I don't think it's possible without human interaction at some point, their way of stopping people from mining it I guess!Muimuir
D
3

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).

Draughtsman answered 12/5, 2015 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.