imcoddy
6/4/2012 - 1:17 PM

sina weibo OAuth with node-oauth for node.js+express

sina weibo OAuth with node-oauth for node.js+express

var express = require('express');
var sys = require('util');
var oauth = require('oauth');

var app = express.createServer();

var config = require('./config')

function consumer() {
  return new oauth.OAuth(
         "http://api.t.sina.com.cn/oauth/request_token", "http://api.t.sina.com.cn/oauth/access_token", 
    config.sina.key, config.sina.secret, "1.0", "http://localhost:9999/sessions/callback", "HMAC-SHA1");   
}

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
  app.use(express.logger());
  app.use(express.cookieParser());
  app.use(express.session({ secret: "string" }));
});

app.dynamicHelpers({
  session: function(req, res){
    return req.session;
  }
});

app.get('/', function(req, res){
  res.send('Hello World');
});

app.get('/sessions/connect', function(req, res){
  consumer().getOAuthRequestToken(function(error, oauthToken, oauthTokenSecret, results){
    console.log(error)
    if (error) {
      res.send("Error getting OAuth request token : " + sys.inspect(error), 500);
    } else {  
      req.session.oauthRequestToken = oauthToken;
      req.session.oauthRequestTokenSecret = oauthTokenSecret;
      res.redirect("https://api.t.sina.com.cn/oauth/authorize?oauth_callback=http://localhost:9999/sessions/callback&oauth_token="+req.session.oauthRequestToken);      
    }
  });
});

app.get('/sessions/callback', function(req, res){
  sys.puts(">>"+req.session.oauthRequestToken);
  sys.puts(">>"+req.session.oauthRequestTokenSecret);
  sys.puts(">>"+req.query.oauth_verifier);
  consumer().getOAuthAccessToken(req.session.oauthRequestToken, req.session.oauthRequestTokenSecret, req.query.oauth_verifier, function(error, oauthAccessToken, oauthAccessTokenSecret, results) {


    sys.puts("error >> "+error);
    sys.puts("oauthAccessToken >> "+oauthAccessToken);
    sys.puts("oauthAccessTokenSecret >> "+oauthAccessTokenSecret);
    sys.puts("results >> "+sys.inspect(results));
    
    
    
    if (error) {
      res.send("Error getting OAuth access token : " + sys.inspect(error) + "["+oauthAccessToken+"]"+ "["+oauthAccessTokenSecret+"]"+ "["+sys.inspect(results)+"]", 500);
    } else {
      req.session.oauthAccessToken = oauthAccessToken;
      req.session.oauthAccessTokenSecret = oauthAccessTokenSecret;
      // Right here is where we would write out some nice user stuff
      consumer().get("http://api.t.sina.com.cn/account/verify_credentials.json", req.session.oauthAccessToken, req.session.oauthAccessTokenSecret, function (error, data, response) {
        data = JSON.parse(data)
        if (error) {
          res.send("Error getting twitter screen name : " + sys.inspect(error), 500);
        } else {
          console.log(sys.inspect(data))
          req.session.sinaScreenName = data["screen_name"];    
          res.send('You are signed in: ' + req.session.sinaScreenName)
        }  
      });  
    }
  });
});

app.listen(parseInt(process.env.PORT || 9999));
{
  "name": "oauth-example"
  , "version": "0.0.1"
  , "private": true
  , "description": "oauth-example"
  , "author": "Scott Ballantyne <ussballantyne@gmail.com>"
  , "dependencies": {
    "express": "*"
    , "oauth": "*"
    
  }
  , "devDependencies": { "vows": "*" }
  
  ,  "engine": "node >= 0.4.1"
}
exports.sina = {key: "key", secret: "secret" }