Skip to content

Instantly share code, notes, and snippets.

@nmoadev
Last active August 29, 2015 14:08
Show Gist options
  • Save nmoadev/12d90bb806e6ddfb8597 to your computer and use it in GitHub Desktop.
Save nmoadev/12d90bb806e6ddfb8597 to your computer and use it in GitHub Desktop.
OAuth in NodeJs with Express
var express = require('express'),
session = require('express-session'),
cookie = require('cookie-parser'),
serveStatic = require('serve-static'),
config,
nodeOAuth;
config = {
requestTokenUrl:'https://oauth.intuit.com/oauth/v1/get_request_token',
accessTokenUrl:'https://oauth.intuit.com/oauth/v1/get_access_token',
authorizeUrl:'https://appcenter.intuit.com/Connect/Begin',
accessTokenCallbackUrl: 'http://localhost:3000/oauth/access_token_ready'
consumerKey: "",
consumerSecret:"",
};
nodeOAuth = require('nodeoauth')(config, function(err, oauthInfo) {
if (err) {
console.log(err);
} else {
console.log(oauthInfo);
}
});
app = express();
app.use(session({secret: 'somesecret'}));
app.use(cookie('some other secret'));
app.set('port', 3000);
app.get('/oauth/request_token', nodeOAuth.requestTokenHandler);
app.get('/oauth/access_token_ready', nodeOAuth.oauthCallback);
app.use(serveStatic('public'));
app.listen(app.get('port'), function() {
console.log('Server started on port: ' + app.get('port'));
});
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ipp="">
<head><meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<title>My Connect Page</title>
<script type="text/javascript" src="https://appcenter.intuit.com/Content/IA/intuit.ipp.anywhere-1.3.0.js">
</script>
<script>intuit.ipp.anywhere.setup({
menuProxy: '',
grantUrl: 'http://localhost:3000/oauth/request_token'});
</script>
</head>
<body>
<ipp:connectToIntuit></ipp:connectToIntuit>
</body>
</html>
var queryString = require('querystring'),
request = require('request'),
windowCloseHtml = '<html><body><script>window.close()</script></body></html>';
module.exports = function(config, oauthFinishedCallback) {
var handlers = {};
handlers.requestOAuthToken = function requestOAuthToken(req, res) {
var requestOptions, requestCallback;
requestOptions = {
url: config.requestTokenUrl,
oauth: {
callback: config.accessTokenCallbackUrl
consumer_key: config.consumerKey,
consumer_secret: config.consumerSecret
}
};
requestCallback = function (_err, _res, _data) {
if (_err) {
oauthFinishedCallback(_err, null);
return;
}
var requestTokenAndSecret = queryString.parse(_data);
req.session.oauth_token_secret = requestTokenAndSecret.oauth_token_secret;
res.redirect(config.authorizeUrl + '?oauth_token=' + requestTokenAndSecret.oauth_token);
};
request.post(requestOptions, requestCallback);
};
:
handlers.requestTokenReady = function requestTokenReady(req, res) {
console.log(req.query);
var requestOptions, requestCallback;
requestOptions = {
url: config.accessTokenUrl,
oauth: {
consumer_key: config.consumerKey,
consumer_secret: config.consumerSecret,
token: req.query.oauth_token,
token_secret: req.session.oauth_token_secret,
verifier: req.query.oauth_verifier,
realmId: req.query.realmId
}
};
requestCallback = function (_err, _res, _data) {
if (_err) {
oauthFinishedCallback(_err, null);
return;
}
var accessTokenAndSecret = queryString.parse(_data);
console.log("AccessTokenAndSecret:");
console.log(accessTokenAndSecret);
config.oauthFinishedCallback(null, {
realmId: req.query.realmId,
accessToken: accessTokenAndSecret.oauth_token,
accessTokenSecert: accessTokenAndSecret.oauth_token_secret
requestToken: req.query.oauth_token,
requestTokenSecret: req.session.oauth_token_secret
});
});
res.send(windowCloseHtml);
};
request.post(requestOptions, requestCallback);
};
return handlers;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment