Comparing version 0.0.0 to 0.0.1
44
gdata.js
var querystring = require('querystring'); | ||
var request = require('request'); | ||
var https = require('https'); | ||
var EventEmitter = require('events').EventEmitter; | ||
@@ -11,3 +12,6 @@ var oauthBase = 'https://accounts.google.com/o/oauth2'; | ||
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) { | ||
@@ -26,2 +30,4 @@ callback(req.query.error); | ||
redirect_uri:redirectURI}, function(err, tkn) { | ||
if(!err && tkn && !tkn.error) | ||
token = tkn; | ||
callback(err, tkn); | ||
@@ -32,3 +38,6 @@ }); | ||
this.getFeed = function(url, params, callback) { | ||
client.setToken = function(tkn) { | ||
token = tkn; | ||
} | ||
client.getFeed = function(url, params, callback) { | ||
if(!callback && typeof params === 'function') { | ||
@@ -38,6 +47,16 @@ callback = 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 { | ||
@@ -55,3 +74,20 @@ body = JSON.parse(body); | ||
} | ||
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; | ||
} | ||
@@ -58,0 +94,0 @@ |
{ | ||
"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>", | ||
@@ -6,0 +6,0 @@ "keywords": ["google", "gdata"], |
@@ -6,3 +6,3 @@ var request = require('request'); | ||
// 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 | ||
@@ -14,5 +14,4 @@ | ||
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) { | ||
@@ -23,3 +22,2 @@ console.error('oh noes!', err); | ||
} else { | ||
token = tkn; | ||
res.redirect('/getStuff'); | ||
@@ -31,3 +29,3 @@ } | ||
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) { | ||
@@ -34,0 +32,0 @@ res.writeHead(200); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18462
452