Вопрос

I'm creating a webapp which requires to connect via Linkedin, I'm using PassportJS to manage the OAuth login. So I managed to do the login, I can retrieve all the information (username, headline, first name, last name and many other stuff) of the connected user.

But now next step, I want to get all the connections of the current user. I have the API url to get all the users, https://api.linkedin.com/v1/people/~/connections?oauth2_access_token=XXXXXXXXXXX Now I'm not sure how to get the oauth2_access_token. In the user object returned by LinkedIn, there is only one token, the x-li-auth-token, but the token's only 5 characters, and that seemed a little weird to me. I tried to put the token inside the URL, but I get this error

<error>
  <status>401</status>
  <timestamp>1396329160196</timestamp>
  <request-id>T32OC18167</request-id>
  <error-code>0</error-code>
  <message>Invalid access token.</message>
</error>

Here are some interesting bits of my code:

In my app.js:

passport.use(new LinkedInStrategy({
      clientID: "xxxxxxx",
      clientSecret: "xxxxxxxx",
      callbackURL: "http://127.0.0.1:3000/auth/callback",
      scope: ['r_fullprofile', 'r_network'],
      }, function(accessToken, refreshToken, profile, done) {
        // asynchronous verification, for effect...
        process.nextTick(function () {
          return done(null, profile);
        });
      }
    ));
passport.serializeUser(function(user, done) {
  done(null, user);
});
passport.deserializeUser(function(obj, done) {
  done(null, obj);
});
app.get('/', routes.index);
app.get('/auth',
  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.
  });
app.get('/auth/callback', passport.authenticate('linkedin', {
  successRedirect: '/',
  failureRedirect: '/auth'
}));
app.listen(3000);

And my index.js

exports.index = function(req, res) {
  var connections;
  var request = require('request');
  var options = 
      { url: 'https://api.linkedin.com/v1/people/~/connections',
        headers: { 'x-li-format': 'json' },
        qs: { oauth2_access_token: "fLz3" } // or &format=json url parameter
      };
  request(options, function ( error, r, body ) {
    if ( r.statusCode != 200 ) {
      return;
    }
    try {
      connections = JSON.parse( body );
    }
    catch (e) {
      return;
    }
  })

  res.render('index', { connections: connections, user: req.user, title: 'Bubble' });
};
Это было полезно?

Решение

You need to use the access_token returned in the verify function:

...
 }, function(accessToken, refreshToken, profile, done) {
...
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top