2012-12-02 96 views
1

我是Express.js,Node.js和Passport.js的新手。我試圖開發一個應用程序,用戶使用他們的Twitter帳戶登錄,然後他們可以看到他們的提及(推文中的其他Twitter用戶提到了登錄用戶的Twitter用戶名)。我遇到了Passport.js,我已經能夠成功地使用它,允許用戶使用他們的Twitter帳戶登錄。HTTP GET請求通過Passport.js和Express.js發送推文

但是,我不知道如何格式化服務器端HTTP GET請求的Twitter提及。我已經在https://dev.twitter.com/docs/api/1/get/statuses/mentions上多次查看過Twitter API,但由於我不熟悉Node/Express/Passport平臺,因此我不確定如何執行此請求服務器端以返回JSON格式的提示。該應用程序設置爲只讀,因爲它只需要能夠看到相關用戶的推文。

前端基於EJS。以下是我所擁有的可能相關的代碼。非常感謝您的幫助。

-joshingmachine

/app.js

/** 
* Module dependencies. 
*/ 

var express = require('express') 
    , passport = require('passport') 
    , http = require('http') 
    , util = require('util') 
    , path = require('path') 
    , TwitterStrategy = require('passport-twitter').Strategy; 

var TWITTER_CONSUMER_KEY = "theConsumerKeyForMyApp"; 
var TWITTER_CONSUMER_SECRET = "theConsumerSecretForMyApp"; 

var users = []; 

// Passport session setup. 
passport.serializeUser(function(user, done) { 
    done(null, user.id); 
}); 

passport.deserializeUser(function(id, done) { 
    var user = users[id]; 
    done(null, user); 
}); 


// Use the TwitterStrategy within Passport. 
passport.use(new TwitterStrategy({ 
    consumerKey: TWITTER_CONSUMER_KEY, 
    consumerSecret: TWITTER_CONSUMER_SECRET, 
    callbackURL: "http://127.0.0.1:3000/auth/twitter/callback" 
    }, 
    function(token, tokenSecret, profile, done) { 
    //console.log(token); 
    //console.log(tokenSecret); 
    //console.log(profile); 
    // asynchronous verification, for effect... 
    process.nextTick(function() { 
     var user = users[profile.id] || (users[profile.id] = profile); 
     done(null, user); 
    }); 
    } 
)); 

var app = express(); 

// configure Express 
app.configure(function(){ 
    app.set('port', process.env.PORT || 3000); 
    app.set('views', __dirname + '/views'); 
    app.set('view engine', 'ejs'); 
    app.use(express.favicon()); 
    app.use(express.logger('dev')); 
    app.use(express.bodyParser()); 
    app.use(express.methodOverride()); 
    app.use(express.cookieParser()); 
    app.use(express.session({ secret:'secret'})); 
    app.use(passport.initialize()); 
    app.use(passport.session()); 
    app.use(app.router); 
    app.use(express.static(__dirname + '/public')); 
}); 


app.get('/', function(req, res){ 
    res.render('index', { user: req.user }); 
}); 

app.get('/account', ensureAuthenticated, function(req, res){ 
    res.render('account', { user: req.user }); 
}); 

app.get('/login', function(req, res){ 
    res.render('login', { user: req.user }); 
}); 

// GET /auth/twitter 
app.get('/auth/twitter', 
    passport.authenticate('twitter'), 
    function(req, res){ 
    // The request will be redirected to Twitter for authentication, so this 
    // function will not be called. 
    }); 

// GET /auth/twitter/callback 
app.get('/auth/twitter/callback', 
    passport.authenticate('twitter', { failureRedirect: '/login' }), 
    function(req, res) { 
    res.redirect('/'); 
    }); 

app.get('/logout', function(req, res){ 
    req.logout(); 
    res.redirect('/'); 
}); 

// Create server 
http.createServer(app).listen(app.get('port'), function(){ 
    console.log("Express server listening on port " + app.get('port')); 
}); 

// Simple route middleware to ensure user is authenticated. 
function ensureAuthenticated(req, res, next) { 
    if (req.isAuthenticated()) { return next(); } 
    res.redirect('/login'); 
} 

app.use(function(req, res, next){ 
    res.send(404, '404 Not Found'); 
}); 

/routes/index.js

/* 
* GET home page. 
*/ 

exports.index = function(req, res){ 
    res.render('index', { title: 'Express' }); 
}; 

回答

2

安裝模塊: $ NPM安裝的OAuth

然後使用此代碼來創建你的GET請求(更換變量需要)。

var oauth = new OAuth.OAuth(
    'https://api.twitter.com/oauth/request_token', 
    'https://api.twitter.com/oauth/access_token', 
    'your application consumer key', 
    'your application secret', 
    '1.0A', 
    null, 
    'HMAC-SHA1' 
); 
oauth.get(
    'https://api.twitter.com/1.1/trends/place.json?id=23424977', 
    'your user toke for this app', //test user token 
    'your user secret for this app', //test user secret    
    function (e, data, res){ 
    if (e) console.error(e);   
    console.log(require('util').inspect(data)); 
    done();  
    });  

如果您需要更多信息,請參考此處。祝你好運! https://github.com/ciaranj/node-oauth

0

我創造了一些類似於你想要做的事情。由於您已成功登錄,因此我假設您能夠獲取訪問令牌和密碼。一旦你有這些,使用像mtwitter外部庫。我打電話是這樣的(編輯出我從db獲得令牌的部分):

app.get('/twitter/userMentions', function (req, res) { 
    var twit = new mtwitter({ 
     consumer_key: consumerKey, 
     consumer_secret: consumerSecret, 
     access_token_key: token, 
     access_token_secret: secret 
    }); 

    twit.get("/statuses/mentions_timeline", { "include_entities": false }, 
     function (err, data) { 
      if (err) { 
       res.write(err.toString()); 
      } 
      else res.write(JSON.stringify(data)); 

      res.end('\n'); 
     }); 
    }