Created
May 23, 2011 08:21
-
-
Save roidrage/986391 to your computer and use it in GitHub Desktop.
Example of using Express and node-oauth to do OAuth with Twitter
This file contains 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
express = require "express" | |
sys = require "sys" | |
util = require "util" | |
oauth = require "oauth" | |
fs = require "fs" | |
app = module.exports = express.createServer() | |
app.configure('development', () -> | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })) | |
app.use(express.logger()) | |
app.use(express.cookieParser()) | |
app.use(express.session({secret: "ssshhhh!"})) | |
) | |
app.configure('production', () -> | |
app.use(express.errorHandler()) | |
app.use(express.logger()) | |
app.use(express.cookieParser()) | |
app.use(express.session({secret: "ssssssssssshhhhh!"})) | |
) | |
configFile = "#{process.env['HOME']}/config.js" | |
config = require configFile | |
consumer = () -> | |
new oauth.OAuth("https://twitter.com/oauth/request_token", | |
"https://twitter.com/oauth/access_token", | |
config["consumerKey"], | |
config["consumerSecret"], | |
"1.0A", | |
"http://host/sessions/callback", | |
"HMAC-SHA1") | |
app.dynamicHelpers({ | |
session: (request, response) -> | |
request.session | |
}) | |
app.get('/', (request, response) -> | |
response.send('Hello World') | |
) | |
app.get '/sessions/connect', (request, response) -> | |
consumer().getOAuthRequestToken (error, oauthToken, oauthTokenSecret, results) -> | |
if error | |
response.send('Error getting OAuth access token') | |
else | |
request.session.oauthRequestToken = oauthToken | |
request.session.oauthRequestTokenSecret = oauthTokenSecret | |
response.redirect("https://twitter.com/oauth/authorize?oauth_token=#{request.session.oauthRequestToken}") | |
app.get '/sessions/callback', (request, response) -> | |
consumer().getOAuthAccessToken request.session.oauthRequestToken, request.session.oauthRequestTokenSecret, request.query.oauth_verifier, (error, oauthAccessToken, oauthAccessTokenSecret, results) -> | |
if error | |
else | |
request.session.oauthAccessToken = oauthAccessToken | |
request.session.oauthAccessTokenSecret = oauthAccessTokenSecret | |
consumer().get "http://twitter.com/account/verify_credential.json", request.session.oauthAccessToken, request.session.oauthAccessTokenSecret, (error, data, response) -> | |
data = JSON.parse(data) | |
response.send("You are signed in: #{data["screen_name"]}") | |
app.listen(parseInt(process.env.PORT || 8080)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ported to CoffeeScript from https://gist.github.com/555607