Permalink
Browse files

fixed oauth middleware to support new api.

  • Loading branch information...
1 parent 3290e80 commit 237c71870f45eac72bbe10d48b5a1a22401f02c8 @fengmk2 fengmk2 committed Sep 28, 2012
View
@@ -1,4 +1,3 @@
language: node_js
node_js:
- - 0.8
- - 0.9
+ - 0.8
@@ -15,8 +15,8 @@ var weibo = require('../');
* init weibo api settings
*/
-weibo.init('weibo', '1306060637', '0850d7407392fb537bff0762406c567d');
-weibo.init('github', '8e14edfda73a71f1f226', '1796ac639a8ada0dff6acfee2d63390440ca0f3b');
+weibo.init('weibo', '1122960051', 'e678e06f627ffe0e60e2ba48abe3a1e3');
+// weibo.init('github', '8e14edfda73a71f1f226', '1796ac639a8ada0dff6acfee2d63390440ca0f3b');
weibo.init('tqq', '801196838', '9f1a88caa8709de7dccbe3cae4bdc962');
/**
View
@@ -1,9 +0,0 @@
-var port = process.env.PORT || 8080;
-require('weibo').start_gtap(port, process.env.HOME);
-console.log('listen on ' + port);
-process.on('uncaughtException', function (err) {
- var util = require('util');
- console.error('Uncaught exception: ' + err);
- console.error(err.message);
- console.error(err.stack);
-});
View
@@ -1,41 +0,0 @@
-var weibo = require('../node-weibo'),
- home_url = 'http://localhost:8024/oauth',
- tapi = weibo.tapi,
- express = require('express'),
- app = express.createServer(),
- port = 8024;
-
-var sinaApp = {
- key : '1306060637',
- secret : '0850d7407392fb537bff0762406c567d',
- blogType: 'tsina'
-}
-
-var qqApp = {
- key : '801004324',
- secret : 'f4dccb3a9f1689adcc66dc933b38445e',
- blogType: 'tqq'
-}
-
-var sohuApp = {
- key : 'geDQ7cFZ7iruNPHm3lZk',
- secret : 'iQ%mtL!eh%xVl!SjQN^($Efdw41!#Ytt*r8SMtw8',
- blogType: 'tsohu'
-}
-
-var apps = [sinaApp , qqApp , sohuApp];
-
-for(var i = 0 ; i < apps.length; i++) {
- tapi.init(apps[i].blogType, apps[i].key, apps[i].secret);
-}
-
-app.listen(port);
-console.log('paint app started on port '+port);
-
-app.use(weibo.oauth_middleware(function(oauth_user, referer, req, res, callback) {
- // do something ...
- // save oauth_user
- console.log(oauth_user);
- callback();
-}));
-
Deleted file not rendered
View
@@ -1,145 +0,0 @@
-/**
- * Module dependencies.
- */
-
-var connect = require('connect')
- , fs = require('fs')
- , urlutil = require('url')
- , tapi = require('../lib/tapi')
- , twitter = tapi.api_dispatch({blogtype: 'twitter'})
- , oauth = require('../lib/oauth_middleware');
-
-exports.start = function(port, store_dir) {
- store_dir = store_dir || process.cwd();
- /**
- * Gtap APPKEY: http://code.google.com/p/gtap/source/browse/trunk/main.py
- * CONSUMER_KEY = 'xzR7LOq6Aeq8uAaGORJHGQ'
- * CONSUMER_SECRET = 'bCgaGEfejtE9mzq5pTMZngjnjd6rRL7hf2WBFjT4'
- */
- tapi.init('twitter', 'Bs03m4guPuksRtIBuvntxw', 'EwwTc3q4xEwxmm9OQnpy7cZdt43HYepeO8wdElGI');
-
- function redirect(res, url) {
- res.writeHead(302, {
- 'Location': url,
- 'Content-Length': 0
- });
- res.end();
- };
-
- // Load the library
- var nStore = require('nstore');
- // Create a store
- var cache = nStore.new(store_dir + '/gtap_oauth_user_cache.db', function (){
- // It's loaded now
- });
-
- var views = {
- '/': fs.readFileSync(__dirname + '/views/index.html'),
- '/setting': fs.readFileSync(__dirname + '/views/setting.html')
- };
-
- var server = connect.createServer(
- connect.bodyParser(),
- connect.static(__dirname + '/public'),
- connect.favicon(),
- connect.cookieParser(),
- connect.session({secret: 'gtap-server-session-secret'}),
- oauth(function(oauth_user, referer, req, res, callback) {
- var key = req.session.user.username + req.session.user.password;
- cache.save(key, oauth_user, function(err) {
- if(err) { console.error(err); }
- redirect(res, '/user_info');
- callback(true);
- });
- }, {default_blogtype: 'twitter'}),
- function(req, res, next) {
- if(req.url == '/') {
- var user = req.session.user;
- if(user) {
- var key = user.username + user.password;
- cache.get(key, function(err, oauth_user, k) {
- if(oauth_user) {
- redirect(res, '/user_info');
- } else {
- res.end(views['/setting']);
- }
- });
- } else {
- res.end(views['/']);
- }
- } else if(req.url == '/login') {
- if(!req.body) {
- return redirect(res, '/');
- }
- var user = {username: req.body.username, password: req.body.password};
- var key = user.username + user.password;
- cache.get(key, function(err, oauth_user, k) {
- if(err) {
- console.log('cache.get ' + key + ' user: ' + user.username + ' error: ' + err.message);
- }
- if(oauth_user) {
- req.session.user = user;
- redirect(res, '/user_info');
- } else {
- req.session.user = user;
- redirect(res, '/');
- }
- });
- } else if(req.url == '/user_info') {
- if(!req.session.user) {
- return redirect(res, '/');
- }
- res.writeHeader(200, {'Content-type': 'text/html'});
- res.end(req.session.user.username + '(' + req.session.user.password
- + ') has connect to twitter. <br />Gtap api: <input type="text" style="width: 300px;" value="http://' + req.headers.host + '/api" />'
- + '<br/><a href="/logout">Logout</a>, you still can use the api proxy.');
- } else if(req.url == '/logout') {
- req.session.user = null;
- redirect(res, '/');
- } else {
- next();
- }
- },
- function(req, res, next) {
- var urlinfo = urlutil.parse(req.url, true);
- if(urlinfo.pathname.indexOf('/api') === 0) {
- urlinfo.pathname = urlinfo.pathname.substring(4);
- }
- var data = urlinfo.query;
- if(req.body) {
- for(var k in req.body) {
- data[k] = req.body[k];
- }
- }
- delete data.source;
- var authorization = req.headers.authorization;
- if (!authorization) return connect.utils.unauthorized(res, '');
-
- var parts = authorization.split(' ')
- , scheme = parts[0]
- , credentials = new Buffer(parts[1], 'base64').toString().split(':');
-
- if ('Basic' != scheme) return connect.utils.badRequest(res);
- var key = credentials[0] + credentials[1];
- cache.get(key, function(err, oauth_user, k) {
- var params = {
- url: urlinfo.pathname,
- play_load: 'string',
- type: req.method,
- user: oauth_user,
- data: data
- };
- twitter._send_request(params, function(err, s, response) {
- if(err) {
- s = JSON.stringify(err);
- }
- res.writeHead(response.statusCode, response.headers);
- res.end(s);
- });
- });
- }
- );
- port = port || 8080;
- server.listen(port);
- console.log('server start listen on ' + port);
-};
View
@@ -1,19 +0,0 @@
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
- <title>Node Gtap API for Twitter</title>
- <meta name="keywords" content="gtap,twitter,api" />
-</head>
-<body>
-<h1>Node Gtap API for Twitter</h1>
-<h2>Setup the username and random password</h2>
-<form action="/login" method="post">
- <label>User Name</label>
- <input name="username" type="text" />
- <label>Password</label>
- <input name="password" type="password" />
- <input type="submit" value="设置" />
-</form>
-</body>
-</html>
View
@@ -1,14 +0,0 @@
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
- <title>Node Gtap API for Twitter</title>
- <meta name="keywords" content="gtap,twitter,api" />
-</head>
-<body>
-<h1>Node Gtap API for Twitter</h1>
-<p>
- Fuck GFW first. <a href="/oauth"><img src="/sign-in-with-twitter-d.png" alt="connect twitter" /></a>
-</p>
-</body>
-</html>
Oops, something went wrong.

0 comments on commit 237c718

Please sign in to comment.