简体   繁体   English

如何在没有重定向的情况下获取LinkedIn API访问令牌

[英]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. 我正在尝试使用LinkedIn的API访问大学的LinkedIn页面,以定期收集他们有多少粉丝。 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! 这似乎可行,但我似乎无法生成一个访问令牌,而没有一些奇怪的重定向URL,必须带你到GUI登录页面!

I'm using node.js for this, specifically this package: https://www.npmjs.org/package/node-linkedin 我正在使用node.js,特别是这个包: 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. 我有一个API密钥和秘密,所以我需要的是一个访问令牌然后我将被设置为实际开始使用他们的API路由。

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. 您要做的是仅应用程序身份验证,似乎linkedIn已删除此选项,不像Facebook和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. 如果你真的想跳过重定向,你可以使用像无头浏览器PhantomJS这样的东西。 But i strongly recommend you not to do so as LinkedIn requires a user to authenticate in their license agreement. 但我强烈建议您不要这样做,因为LinkedIn要求用户在其许可协议中进行身份验证。 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). 我不知道它是否合法,但您可以为自己提供一个终点,用于生成authentication_code和access_token,然后将其保存到数据库(默认情况下有效期为60天)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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