简体   繁体   中英

Trying to connect to LinkedIn API with Node.js Express

I'm having trouble connecting to, and pulling connections from, Linkedin . I have to use nodejs and express , and am using the linkedin-js client ( https://github.com/masylum/linkedin-js ). I have looked everywhere for examples and still don't quite understand how to do the "get" call and display a users connections on the page. I'm getting an error when I use "render", but when I try to use "send" instead, I find that the API call isn't working anyways. Anyone's help and explanation would be greatly appreciated! Here's what I have so far.

var express = require('express')
, linkedin_client = require('linkedin-js')('xxx', 'xxx', 'http://localhost:3003/')
, app = express();

app.configure(function(){

app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/'));
app.use(express.cookieParser());
app.use(express.session({ secret: "string" }));  
});
app.get('/', function (req, res) {
// the first time will redirect to linkedin
 linkedin_client.getAccessToken(req, res, function (error, token) {
// will enter here when coming back from linkedin
req.session.token = token;


 linkedin_client.apiCall('GET', '/people/~/connections:(headline,first-name,last-name)',
{
  token: {
    oauth_token_secret: req.session.token.oauth_token_secret
  , oauth_token: req.session.token.oauth_token
  }
, fields: req.param('id')//not sure?
}
, function (error, result) {
  res.send('message_sent');
}
);
//res.render('auth');  I'm getting an error here as well
});
});



app.post('/message', function (req, res) {
linkedin_client.apiCall('POST', '/people/~/shares',
{
  token: {
    oauth_token_secret: req.session.token.oauth_token_secret
  , oauth_token: req.session.token.oauth_token
  }
, share: {
    comment: req.param('message')
  , visibility: {code: 'anyone'}
  }
}
, function (error, result) {
  res.render('message_sent');
}
);
});





app.listen(3003);
console.log("Listening");

On the second line replace the 'API Key' and 'Secret Key' with your own from https://www.linkedin.com/secure/developer :

 var express = require('express'), linkedin_client = require('linkedin-js')('API Key', 'Secret Key', 'http://localhost:3003/'), bodyParser = require('body-parser'), cookieParser = require('cookie-parser'), methodOverride = require('method-override'), session = require('express-session'), app = express(); app.use(bodyParser.json()); //basic app set up app.use(bodyParser.urlencoded({extended: true})); app.use(methodOverride()); app.use(express.static(__dirname + '/')); app.use(cookieParser()); app.use(session({secret: 'secret', saveUninitialized: true, resave: true})); //session stuff app.get('/', function (req, res) { // the first time will redirect to linkedin linkedin_client.getAccessToken(req, res, function (error, token) { // will enter here when coming back from linkedin req.session.token = token; linkedin_client.apiCall('GET', '/people/~/connections:(headline,first-name,last-name)', { token: { oauth_token_secret: req.session.token.oauth_token_secret , oauth_token: req.session.token.oauth_token } , fields: req.param('id')//not sure? } , function (error, result) { res.send(result); } ); }); }); app.post('/message', function (req, res) { linkedin_client.apiCall('POST', '/people/~/shares', { token: { oauth_token_secret: req.session.token.oauth_token_secret , oauth_token: req.session.token.oauth_token } , share: { comment: req.param('message') , visibility: {code: 'anyone'} } } , function (error, result) { res.render('message_sent'); } ); }); app.listen(3003); console.log("Listening"); 

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