Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
7kai Tasks OAuth sample code for Node.js.
var express = require('express')
, http = require('http')
, path = require('path')
, OAuth = require('oauth').OAuth;
var app = 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: 'dummy'}));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
var oa = new OAuth(
'https://tasks.7kai.org/oauth/request_token',
'https://tasks.7kai.org/oauth/access_token',
'YOUR_APP_CONSUMER_KEY',
'YOUR_APP_CONSUMER_SECRET',
'1.0',
'http://127.0.0.1:3000/callback',
'HMAC-SHA1'
);
app.get('/', function(req, res){
oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){
if (error) {
console.log(error);
res.send('yeah no. didn\'t work.')
}
else {
req.session.oauth = {};
req.session.oauth.token = oauth_token;
req.session.oauth.token_secret = oauth_token_secret;
res.redirect('https://tasks.7kai.org/oauth/authorize?oauth_token=' + oauth_token)
}
});
});
app.get('/callback', function(req, res, next){
if (req.session.oauth) {
req.session.oauth.verifier = req.query.oauth_verifier;
var oauth = req.session.oauth;
oa.getOAuthAccessToken(
oauth.token,
oauth.token_secret,
req.session.oauth.verifier,
function(error, oauth_access_token, oauth_access_token_secret, results){
if (error){
console.log(error);
res.send('yeah something broke.');
} else {
req.session.oauth.access_token = oauth_access_token;
req.session.oauth.access_token_secret = oauth_access_token_secret;
oa.getProtectedResource(
'https://tasks.7kai.org/api/1/account/me',
'GET',
req.session.oauth.access_token,
req.session.oauth.access_token_secret,
function(error, data, response){
var json = JSON.parse(data);
res.send(data);
}
);
}
}
);
} else
next(new Error('you\'re not supposed to be here.'))
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment