Created
March 1, 2012 15:41
-
-
Save peterjmit/1950591 to your computer and use it in GitHub Desktop.
Node.js Twitter/Facebook API JSONP Proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var express = require('express'), | |
| graph = require('fbgraph'), | |
| oauth = require('oauth'); | |
| var app = module.exports = express.createServer(); | |
| var twConfig = { | |
| consumer_key: 'CONSUMER_KEY', | |
| consumer_secret: 'CONSUMER_SECRET', | |
| request_token_url: 'https://api.twitter.com/oauth/request_token', | |
| authorize_url: 'https://api.twitter.com/oauth/authorize', | |
| access_token_url: 'https://api.twitter.com/oauth/access_token' | |
| }; | |
| function consumer(callbackUrl) { | |
| if(callbackUrl === undefined) { | |
| callbackUrl = null; | |
| } | |
| return new oauth.OAuth( | |
| twConfig.request_token_url, | |
| twConfig.access_token_url, | |
| twConfig.consumer_key, | |
| twConfig.consumer_secret, | |
| "1.0A", | |
| callbackUrl, | |
| "HMAC-SHA1" | |
| ); | |
| } | |
| app.configure(function(){ | |
| app.use(express.bodyParser()); | |
| app.use(express.methodOverride()); | |
| app.use(app.router); | |
| }); | |
| app.get('/fb-post', function(req, res) { | |
| res.statusCode = 200; | |
| console.log(req.query); | |
| var body = {}, | |
| params = req.query; | |
| if(params.access_token === undefined || params.message === undefined) { | |
| body.error = { | |
| message: 'The request must contain an access token and a message', | |
| params: params | |
| }; | |
| body = params.callback + '(' + JSON.stringify(body) + ')'; | |
| res.setHeader('Content-Length', body.length); | |
| res.setHeader('Content-Type', 'application/json'); | |
| res.end(body); | |
| } else { | |
| var access_token = params.access_token, | |
| message = params.message; | |
| graph.setAccessToken(access_token); | |
| graph.post('/me/feed', { message: message }, function(err, result) { | |
| if(err) { | |
| body.error = err; | |
| } else { | |
| body.result = { | |
| message: result, | |
| request: params | |
| }; | |
| } | |
| body = params.callback + '(' + JSON.stringify(body) + ')'; | |
| res.setHeader('Content-Length', body.length); | |
| res.setHeader('Content-Type', 'application/javascript'); | |
| res.end(body); | |
| }); | |
| } | |
| }); | |
| app.get('/tw-request-token', function(req, res) { | |
| var body = {}, | |
| params = req.query; | |
| if(params.callbackUrl === undefined) { | |
| body.error = { | |
| message: 'The request must contain a callback url', | |
| params: params | |
| }; | |
| body = params.callback + '(' + JSON.stringify(body) + ')'; | |
| res.setHeader('Content-Length', body.length); | |
| res.setHeader('Content-Type', 'application/json'); | |
| res.end(body); | |
| } else { | |
| // Get a request token and send it back to the mobile | |
| consumer(params.callbackUrl) | |
| .getOAuthRequestToken(function(error, token, secret, results) { | |
| if (error || ! results.oauth_callback_confirmed ) { | |
| body.error = { | |
| message: error, | |
| params: params, | |
| result: results | |
| }; | |
| } else { | |
| body = { | |
| oauth_token: token, | |
| oauth_secret: secret, | |
| redirectUrl: twConfig.authorize_url + '?oauth_token=' + token | |
| }; | |
| } | |
| console.log(error); | |
| console.log(results); | |
| body = params.callback + '(' + JSON.stringify(body) + ')'; | |
| res.setHeader('Content-Length', body.length); | |
| res.setHeader('Content-Type', 'application/json'); | |
| res.end(body); | |
| }); | |
| } | |
| }); | |
| app.get('/tw-access-token', function(req, res) { | |
| var body = {}, | |
| params = req.query; | |
| if(params.oauth_token === undefined || params.oauth_secret === undefined || params.oauth_verifier === undefined) { | |
| body.error = { | |
| message: 'The request must contain a callback url', | |
| params: params | |
| }; | |
| body = params.callback + '(' + JSON.stringify(body) + ')'; | |
| res.setHeader('Content-Length', body.length); | |
| res.setHeader('Content-Type', 'application/json'); | |
| res.end(body); | |
| } else { | |
| consumer() | |
| .getOAuthAccessToken( | |
| params.oauth_token, | |
| params.oauth_secret, | |
| params.oauth_verifier, | |
| function(error, token, secret, results) { | |
| if (error) { | |
| body.error = { | |
| message: error, | |
| params: params, | |
| result: results | |
| }; | |
| } else { | |
| body = { | |
| oauth_token: token, | |
| oauth_secret: secret | |
| }; | |
| } | |
| body = params.callback + '(' + JSON.stringify(body) + ')'; | |
| res.setHeader('Content-Length', body.length); | |
| res.setHeader('Content-Type', 'application/json'); | |
| res.end(body); | |
| }); | |
| } | |
| }); | |
| app.get('/tw-verify', function(req, res) { | |
| var body = {}, | |
| params = req.query; | |
| if(params.oauth_token === undefined || params.oauth_secret === undefined) { | |
| body.error = { | |
| status: 400, | |
| message: 'The request must contain an oauth token and secret', | |
| params: params | |
| }; | |
| body = params.callback + '(' + JSON.stringify(body) + ')'; | |
| res.setHeader('Content-Length', body.length); | |
| res.setHeader('Content-Type', 'application/json'); | |
| res.end(body); | |
| } else { | |
| consumer().get('http://api.twitter.com/1/account/verify_credentials.json', | |
| params.oauth_token, | |
| params.oauth_secret, | |
| function(error, result) { | |
| if (error) { | |
| body.error = { | |
| status: error.statuscode, | |
| message: JSON.parse(error.data).error, | |
| params: params, | |
| result: result | |
| }; | |
| } else { | |
| body = { | |
| result: JSON.parse(result) | |
| }; | |
| } | |
| console.log(error); | |
| console.log(result); | |
| body = params.callback + '(' + JSON.stringify(body) + ')'; | |
| res.setHeader('Content-Length', body.length); | |
| res.setHeader('Content-Type', 'application/json'); | |
| res.end(body); | |
| }); | |
| } | |
| }); | |
| app.get('/tw-post', function(req, res) { | |
| var body = {}, | |
| params = req.query; | |
| if(params.oauth_token === undefined || params.oauth_secret === undefined || params.message === undefined) { | |
| body.error = { | |
| status: 400, | |
| message: 'The request must contain an access token and a message', | |
| params: params | |
| }; | |
| body = params.callback + '(' + JSON.stringify(body) + ')'; | |
| res.setHeader('Content-Length', body.length); | |
| res.setHeader('Content-Type', 'application/json'); | |
| res.end(body); | |
| } else { | |
| var msg = { "status" : params.message }; | |
| consumer().post("http://api.twitter.com/1/statuses/update.json", | |
| params.oauth_token, | |
| params.oauth_secret, | |
| msg, | |
| function(error, result) { | |
| if (error) { | |
| body.error = { | |
| status: error.statuscode, | |
| message: JSON.parse(error.data).error, | |
| params: params, | |
| result: result | |
| }; | |
| } else { | |
| body = { | |
| result: result | |
| }; | |
| } | |
| console.log(error); | |
| console.log(result); | |
| body = params.callback + '(' + JSON.stringify(body) + ')'; | |
| res.setHeader('Content-Length', body.length); | |
| res.setHeader('Content-Type', 'application/json'); | |
| res.end(body); | |
| }); | |
| } | |
| }); | |
| app.listen(3000); | |
| console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment