Browse files

added token refresh handling

  • Loading branch information...
1 parent 8b8387e commit 2c62a45c5dafbd2faf376009e46ee56c188197e7 @smurthas committed Jun 10, 2011
Showing with 45 additions and 11 deletions.
  1. +40 −4 gdata.js
  2. +2 −2 package.json
  3. +3 −5 test.js
View
44 gdata.js
@@ -1,14 +1,18 @@
var querystring = require('querystring');
var request = require('request');
var https = require('https');
+var EventEmitter = require('events').EventEmitter;
var oauthBase = 'https://accounts.google.com/o/oauth2';
module.exports = function(client_id, client_secret, redirect_uri) {
var clientID = client_id;
var clientSecret = client_secret;
var redirectURI = redirect_uri;
- this.getAccessToken = function(scope, req, res, callback) {
+ var token;
+
+ var client = new EventEmitter();
+ client.getAccessToken = function(scope, req, res, callback) {
if(req.query.error) {
callback(req.query.error);
} else if(!req.query.code) {
@@ -22,20 +26,35 @@ module.exports = function(client_id, client_secret, redirect_uri) {
client_id:clientID,
client_secret:clientSecret,
redirect_uri:redirectURI}, function(err, tkn) {
+ if(!err && tkn && !tkn.error)
+ token = tkn;
callback(err, tkn);
});
}
}
- this.getFeed = function(url, params, callback) {
+ client.setToken = function(tkn) {
+ token = tkn;
+ }
+ client.getFeed = function(url, params, callback) {
if(!callback && typeof params === 'function') {
callback = params;
params = {};
}
+ params.oauth_token = token.access_token;
params.alt = 'json';
var reqUrl = url + '?' + querystring.stringify(params);
request.get({uri:reqUrl}, function(err, resp, body) {
- if(!err && body) {
+ if(resp.statusCode === 401) {
+ console.error('401 baby!!');
+ refreshToken(function(err, result) {
+ if(!err && result && !result.error && result.access_token) {
+ token.access_token = result.access_token;
+ client.emit('tokenRefresh');
+ client.getFeed(url, params, callback);
+ }
+ });
+ } else if(!err && body) {
try {
body = JSON.parse(body);
} catch(e) {
@@ -48,7 +67,24 @@ module.exports = function(client_id, client_secret, redirect_uri) {
}
});
}
- return this;
+
+ function refreshToken(callback) {
+ doPost({client_id:clientID,
+ client_secret:clientSecret,
+ refresh_token:token.refresh_token,
+ grant_type:'refresh_token'
+ }, function(err, result) {
+ if(!err && result && result.access_token) {
+ token.access_token
+ result.refresh_token = token.refresh_token;
+ } else {
+ console.error('err', err);
+ console.error('result', result);
+ }
+ callback(err, result);
+ });
+ }
+ return client;
}
function doPost(body, callback) {
View
4 package.json
@@ -1,11 +1,11 @@
{
"name": "gdata-js",
"description": "Simple OAuth 2.0 GData API client",
- "version": "0.0.0",
+ "version": "0.0.1",
"author": "Simon Murtha-Smith <simon@murtha-smith.com>",
"keywords": ["google", "gdata"],
"main" : "gdata.js",
"dependencies": {},
"repository" : {"type": "git" , "url": "http://github.com/smurthas/gdata-js.git" },
- "engines": { "node": ">=0.4.6 <0.4.8" }
+ "engines": { "node": ">=0.4.6 <=0.4.8" }
}
View
8 test.js
@@ -3,29 +3,27 @@ var querystring = require('querystring');
var fs = require('fs');
// get an clientID and clientSecret at https://code.google.com/apis/console/
-var gdata = require('./gdata')('yourClientID', 'yourClientSecret', 'http://localhost:8553/')
+var gdataClient = require('./gdata')('yourClientID', 'yourClientSecret', 'http://localhost:8553/')
var scope = 'https://www.google.com/m8/feeds/'; //contacts
var express = require('express'),
connect = require('connect'),
app = express.createServer(connect.bodyParser());
-var token;
app.get('/', function (req, res) {
- gdata.getAccessToken(scope, req, res, function(err, tkn) {
+ gdataClient.getAccessToken(scope, req, res, function(err, token) {
if(err) {
console.error('oh noes!', err);
res.writeHead(500);
res.end('error: ' + JSON.stringify(err));
} else {
- token = tkn;
res.redirect('/getStuff');
}
});
});
app.get('/getStuff', function(req, res) {
- getFeed('https://www.google.com/m8/feeds/contacts/default/full', {oauth_token:token.access_token, 'max-results':3},
+ gdataClient.getFeed('https://www.google.com/m8/feeds/contacts/default/full', {'max-results':3},
function(err, feed) {
res.writeHead(200);
for(var i in feed.feed.entry) {

0 comments on commit 2c62a45

Please sign in to comment.