Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow complete customization of oauth options (via https://developers…

….google.com/accounts/docs/OAuth2WebServer) in the getAccessToken function, by expanding the first paramter from just a scope string to the full set params
  • Loading branch information...
commit 86c64bf568b1b58a300c631dc027168a94ccbf5d 1 parent a54b166
Simon Murtha Smith authored
Showing with 34 additions and 15 deletions.
  1. +20 −11 gdata.js
  2. +1 −1  package.json
  3. +13 −3 test.js
31 gdata.js
View
@@ -11,19 +11,23 @@ module.exports = function(client_id, client_secret, redirect_uri) {
var clientSecret = client_secret;
var redirectURI = redirect_uri;
var token;
-
+
var client = new EventEmitter();
- client.getAccessToken = function(scope, req, res, callback) {
+ client.getAccessToken = function(options, req, res, callback) {
if(req.query.error) {
callback(req.query.error);
} else if(!req.query.code) {
+ options.client_id = clientID;
+ options.redirect_uri = options.redirect_uri || redirectURI;
+ options.response_type = 'code';
var height = 750;
var width = 980;
- resp = "<script type='text/javascript'>var left= (screen.width / 2) - (" + width + " / 2); var top = (screen.height / 2) - (" + height + " / 2); window.open('" + oauthBase + '/auth?' + querystring.stringify({client_id: clientID, redirect_uri: redirectURI, scope: scope, response_type: 'code'}) + "', 'auth', 'menubar=no,toolbar=no,status=no,width=" + width + ",height=" + height + ",toolbar=no,left=' + left + 'top=' + top);</script>";
- res.end(resp + '<a target=_new href=\'' + oauthBase + '/auth?' + querystring.stringify({client_id: clientID ,
- redirect_uri: redirectURI,
- scope: scope,
- response_type: 'code'}) + '\'>Authenticate</a>');
+ resp = "<script type='text/javascript'>" +
+ "var left= (screen.width / 2) - (" + width + " / 2);" +
+ "var top = (screen.height / 2) - (" + height + " / 2);" +
+ "window.open('" + oauthBase + '/auth?' + querystring.stringify(options) + "', 'auth', 'menubar=no,toolbar=no,status=no,width=" + width + ",height=" + height + ",toolbar=no,left=' + left + 'top=' + top);" +
+ "</script>";
+ res.end(resp + '<a target=_new href=\'' + oauthBase + '/auth?' + querystring.stringify(options) + '\'>Authenticate</a>');
} else {
doPost({grant_type:'authorization_code',
code:req.query.code,
@@ -36,10 +40,11 @@ module.exports = function(client_id, client_secret, redirect_uri) {
});
}
}
-
+
client.setToken = function(tkn) {
token = tkn;
}
+
client.getFeed = function(url, params, callback) {
if(!callback && typeof params === 'function') {
callback = params;
@@ -52,8 +57,8 @@ module.exports = function(client_id, client_secret, redirect_uri) {
callback(err, body);
});
};
-
-
+
+
function doRequest(url, params, callback) {
var path = URL.parse(url).pathname + '?' + querystring.stringify(params);
var options = {
@@ -92,7 +97,7 @@ module.exports = function(client_id, client_secret, redirect_uri) {
});
httpsReq.end();
}
-
+
function refreshToken(callback) {
doPost({client_id:clientID,
client_secret:clientSecret,
@@ -106,6 +111,10 @@ module.exports = function(client_id, client_secret, redirect_uri) {
callback(err, result);
});
}
+
+ //for debugging
+ client._refreshToken = refreshToken;
+
return client;
}
2  package.json
View
@@ -1,7 +1,7 @@
{
"name": "gdata-js",
"description": "Simple OAuth 2.0 GData API client",
- "version": "1.0.0",
+ "version": "2.0.0",
"author": "Simon Murtha-Smith <simon@murtha-smith.com>",
"keywords": ["google", "gdata"],
"main" : "gdata.js",
16 test.js
View
@@ -2,16 +2,26 @@ var request = require('request');
var querystring = require('querystring');
var fs = require('fs');
+var clientID = process.argv[2];
+var clientSecret = process.argv[3];
+
+if (! (clientID && clientSecret) ) {
+ console.error('usage: node test.js <clientID> <clientSecret>');
+ process.exit(1);
+}
// get an clientID and clientSecret at https://code.google.com/apis/console/
-var gdataClient = require('./gdata')('yourClientID', 'yourClientSecret', 'http://localhost:8553/')
+var gdataClient = require('./gdata')(clientID, clientSecret, 'http://localhost:8553/');
var scope = 'https://www.google.com/m8/feeds/'; //contacts
var express = require('express'),
connect = require('connect'),
app = express.createServer(connect.bodyParser());
-
+
app.get('/', function (req, res) {
- gdataClient.getAccessToken(scope, req, res, function(err, token) {
+ // see https://developers.google.com/accounts/docs/OAuth2WebServer for options
+ gdataClient.getAccessToken({scope: scope,
+ access_type: 'offline',
+ approval_prompt: 'force'}, req, res, function(err, token) {
if(err) {
console.error('oh noes!', err);
res.writeHead(500);
Please sign in to comment.
Something went wrong with that request. Please try again.