尝试使用Node.js Express连接到LinkedIn API

时间:2012-12-24 06:53:34

标签: javascript api node.js linkedin

我无法连接到Linkedin并从中提取连接。我必须使用nodejsexpress,并使用linkedin-js客户端(https://github.com/masylum/linkedin-js)。我到处寻找示例,但仍然不太明白如何进行“获取”调用并在页面上显示用户连接。当我使用“render”时出现错误,但是当我尝试使用“send”时,我发现API调用无论如何都无法正常工作。任何人的帮助和解释将不胜感激!这是我到目前为止所拥有的。

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");

1 个答案:

答案 0 :(得分:0)

在第二行,用https://www.linkedin.com/secure/developer替换'API Key'和'Secret Key':

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");