简体   繁体   中英

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('/');
  });
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM