Skip to content

Instantly share code, notes, and snippets.

@spencerhunter
Created January 26, 2015 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spencerhunter/20a5d3081211a8eeddb7 to your computer and use it in GitHub Desktop.
Save spencerhunter/20a5d3081211a8eeddb7 to your computer and use it in GitHub Desktop.
Dwolla OAuth flow with node.js
var express = require('express');
var router = express.Router();
var request = require('request');
var c = require('../config');
//step 1
var redirect_uri = c.host + '/oauth_return';
router.get('/auth/dwolla', function(req, res) {
var scope = 'Transactions';
var client_id = c.client_id;
var url = util.format("https://www.dwolla.com/oauth/v2/authenticate?client_id=%s&response_type=code&redirect_uri=%s&scope=%s",
encodeURIComponent(client_id),
encodeURIComponent(redirect_uri),
encodeURIComponent(scope));
res.redirect(url);
});
//step 2 & 3
router.get('/oauth_return', function(req, res) {
var client_id = c.client_id;
var client_secret = c.client_secret;
var code = req.query.code;
var url = util.format("https://www.dwolla.com/oauth/v2/token?client_id=%s&client_secret=%s&grant_type=authorization_code&redirect_uri=%s&code=%s",
encodeURIComponent(client_id),
encodeURIComponent(client_secret),
encodeURIComponent(redirect_uri),
encodeURIComponent(code));
request(url, function(error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body);
req.session.access_token = data.access_token;
console.log(req.session.access_token);
res.redirect('/transactions');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment