Permalink
Browse files

hello, gdata

  • Loading branch information...
smurthas committed Jun 10, 2011
0 parents commit 6d16a708e5eaf258c9f066a0418f3857acbc7046
Showing with 525 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +23 −0 README.md
  3. +72 −0 gdata.js
  4. +359 −0 node_modules/request/main.js
  5. +14 −0 node_modules/request/package.json
  6. +11 −0 package.json
  7. +42 −0 test.js
@@ -0,0 +1,4 @@
+.DS_Store
+node_modules/.bin
+node_modules/express
+node_modules/connect
@@ -0,0 +1,23 @@
+# gdata-js
+
+Simple Google Data API client for OAuth 2.0 with express + connect.
+
+ npm install gdata-js
+
+## Usage
+
+gdata-js has two methods:
+
+* getAccessToken(_req_, _res_, _callback_): Goes through the OAuth 2.0 flow to get an access token
+* getFeed(_http_method_, _path_, _params_, _callback_): Does a call to the Google Data API to get a feed object.
+
+oauth_token must be contained in the _params_ argument as demonstrated in test.js
+
+## Test
+
+Enter your consumer key and secret in test.js
+
+ cd test
+ node test.js
+
+open http://localhost:8553
@@ -0,0 +1,72 @@
+var querystring = require('querystring');
+var request = require('request');
+var https = require('https');
+
+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) {
+ if(req.query.error) {
+ callback(req.query.error);
+ } else if(!req.query.code) {
+ res.redirect(oauthBase + '/auth?' + querystring.stringify({client_id: clientID ,
+ redirect_uri: redirectURI,
+ scope: scope,
+ response_type: 'code'}));
+ } else {
+ doPost({grant_type:'authorization_code',
+ code:req.query.code,
+ client_id:clientID,
+ client_secret:clientSecret,
+ redirect_uri:redirectURI}, function(err, tkn) {
+ callback(err, tkn);
+ });
+ }
+ }
+
+ this.getFeed = function(url, params, callback) {
+ if(!callback && typeof params === 'function') {
+ callback = params;
+ params = {};
+ }
+ params.alt = 'json';
+ var reqUrl = url + '?' + querystring.stringify(params);
+ request.get({uri:reqUrl}, function(err, resp, body) {
+ if(!err && body) {
+ try {
+ body = JSON.parse(body);
+ } catch(e) {
+ callback(e, body);
+ return;
+ }
+ callback(null, body);
+ } else {
+ callback(err, body);
+ }
+ });
+ }
+ return this;
+}
+
+function doPost(body, callback) {
+ var options = {
+ host: 'accounts.google.com',
+ port: 443,
+ path: '/o/oauth2/token',
+ method: 'POST',
+ headers: {'Content-Type':'application/x-www-form-urlencoded'}
+ };
+ var httpsReq = https.request(options, function(httpsRes) {
+ httpsRes.on('data', function(data) {
+ callback(null, JSON.parse(data));
+ });
+ });
+ httpsReq.write(querystring.stringify(body));
+ httpsReq.on('error', function(e) {
+ callback(e, null);
+ });
+ httpsReq.end();
+}
Oops, something went wrong.

0 comments on commit 6d16a70

Please sign in to comment.