Permalink
Browse files

Updating example to use data

  • Loading branch information...
1 parent 232e12c commit 18455fcc031826c6147d2cb4f470fcd68a92f384 @jimib committed Feb 24, 2013
Showing with 101 additions and 50 deletions.
  1. +30 −30 examples/example-auth.js
  2. +67 −16 examples/example-get-contacts.js
  3. +3 −3 lib/api.js
  4. +1 −1 package.json
View
@@ -1,5 +1,9 @@
var gAPI = require('../index').API;
-var fs = require("fs");
+var fs = require("fs"),
+ util = require("util");
+
+var scope = 'https://www.google.com/m8/feeds/'; //contacts
+
//put your authentication stuff in 'auth.conf'
var auth = fs.readFile("./auth.conf", function(err, data){
if(data){
@@ -17,39 +21,35 @@ var auth = fs.readFile("./auth.conf", function(err, data){
var express = require('express');
var app = express();
var port = 4000;
-
- app.get('/', function(req, res){
- res.send('hello');
- });
+ var page_cb_auth = '/authenticate';
- app.get('/oauth2callback', function(req, res){
- if(!req.query.code){
- //Redirect the user to Authentication From
- api.oauth.getGoogleAuthorizeTokenURL( ['https://www.googleapis.com/auth/contacts'], function(err, redirecUrl) {
- if(err) return res.send(500,err);
- return res.redirect(redirecUrl);
- });
-
- }else{
- //Get access_token from the code
- api.oauth.getGoogleAccessToken(req.query, function(err, access_token, refresh_token) {
- if(err) return res.send(500,err);
-
- console.log("access_token", access_token);
- console.log("refresh_token", refresh_token);
-
- return res.redirect('/');
- });
- }
- });
-
- console.log("gAPI", gAPI);
-
var api = new gAPI(
data.consumer_key,
data.consumer_secret,
- 'http://localhost:'+port+'/oauth2callback');
-
+ 'http://localhost:'+port+'/authenticate');
+
+ app.get(page_cb_auth, function(req, res){
+ api.gdata.getAccessToken(
+ {
+ scope: scope,
+ access_type: 'offline',
+ approval_prompt: 'force'
+ },
+ req,
+ res,
+ function(err, _token) {
+ if(err) {
+ console.error('oh noes!', err);
+ res.writeHead(500);
+ res.end('error: ' + JSON.stringify(err));
+ } else {
+ token = _token;
+ console.log('got token:', token);
+ res.send(token);
+ }
+ });
+ });
+
app.listen(port);
}
@@ -1,5 +1,9 @@
var gAPI = require('../index').API;
-var fs = require("fs");
+var fs = require("fs"),
+ util = require("util");
+
+var scope = 'https://www.google.com/m8/feeds/'; //contacts
+
//put your authentication stuff in 'auth.conf'
var auth = fs.readFile("./auth.conf", function(err, data){
if(data){
@@ -17,24 +21,71 @@ var auth = fs.readFile("./auth.conf", function(err, data){
var express = require('express');
var app = express();
var port = 4000;
-
- app.get('/', function(req, res){
- //get my list of contacts
- var email = encodeURIComponent("jimi.bailey@gmail.com");
- var urlRequest = "https://www.google.com/m8/feeds/contacts/"+email+"/full";
-
- api.oauth._request("GET", urlRequest, {"GData-Version" : "3.0"}, "", data.access_token, function(result){
- res.send(result.data);
- });
- });
-
- console.log("gAPI", gAPI);
-
+ var page_cb_auth = '/authenticate';
+
var api = new gAPI(
data.consumer_key,
data.consumer_secret,
- 'http://localhost:'+port+'/oauth2callback');
-
+ 'http://localhost:'+port+'/authenticate');
+
+ api.gdata.setToken(data.token);
+
+ app.get("/", function(req, res){
+ api.gdata.getFeed(
+ 'https://www.google.com/m8/feeds/contacts/default/full',
+ {'max-results':500,'group':'http://www.google.com/m8/feeds/groups/default/base/6'},
+ function(err, feed) {
+ res.send(feed);
+ });
+ });
+
+ app.get("/groups", function(req, res){
+ api.gdata.getFeed(
+ 'https://www.google.com/m8/feeds/groups/default/full',
+ {},
+ function(err, feed) {
+ res.send(feed);
+ });
+ });
+
+ app.get("/group/:groupId", function(req, res){
+ api.gdata.getFeed(
+ 'https://www.google.com/m8/feeds/groups/default/full/'+req.params.groupId,
+ {},
+ function(err, feed) {
+ res.send(feed);
+ });
+ });
+
+ app.get("/", function(req, res){
+ api.gdata.getFeed(
+ 'https://www.google.com/m8/feeds/contacts/groups/default/full',
+ {},
+ function(err, feed) {
+ res.send(feed);
+ });
+ });
+
+ app.get("/contact/:id", function(req, res){
+ api.gdata.getFeed(
+ 'https://www.google.com/m8/feeds/contacts/jimi.bailey%40gmail.com/full/'+req.params.id,
+ {},
+ function(err, feed) {
+ console.log("err: ", err);
+ res.send(feed);
+ });
+ });
+
+ app.get("/search/:search", function(req, res){
+ api.gdata.getFeed(
+ 'https://www.google.com/m8/feeds/contacts/default/full',
+ {q:req.params.search},
+ function(err, feed) {
+ console.log(err);
+ res.send(feed);
+ });
+ });
+
app.listen(port);
}
View
@@ -1,11 +1,11 @@
-var OAuth = require("google-oauth"),
+var gdata = require('gdata-js'),
Request = require("./request");
var API = module.exports = function(consumer_key, consumer_secret, callback_uri){
var self = this;
- self.oauth = new OAuth.OAuth2(consumer_key, consumer_secret, callback_uri);
+ self.gdata = new gdata(consumer_key, consumer_secret, callback_uri);
//generate the parts to the
- self.request = new Request(self.oauth);
+ self.request = new Request(self.gdata);
}
View
@@ -8,6 +8,6 @@
},
"version": "0.0.1",
"dependencies": {
- "google-oauth":"0.0.2"
+ "gdata-js":"3.0.0"
}
}

0 comments on commit 18455fc

Please sign in to comment.