Skip to content

Instantly share code, notes, and snippets.

@Ajnasz
Created February 9, 2011 21:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ajnasz/819320 to your computer and use it in GitHub Desktop.
Save Ajnasz/819320 to your computer and use it in GitHub Desktop.
var https = require('https'),
events = require('events');
const userAgent = 'GCLNodejs';
const version = 0.1;
const loginURL = '/accounts/ClientLogin';
const googleHost = 'www.google.com';
/**
* Helps to log in to any google service with the clientlogin method
* Google returns 3 values when login was success:
* Auth, SID, LSID
*
* After the login you need to include the Auth value into
* the Authorization HTTP header on each request:
*
* client.request('GET', '...', {
* ...,
* 'Authorization':'GoogleLogin auth=' + googleClientLoginInstance.getAuthId()
* })
*
* @class GoogleClientLogin
* @constructor
* @param Object conf An object, with two properties: email and password
*/
var GoogleClientLogin = function(conf) {
this.conf = conf || {};
// stores the authentication data
this.auths = {};
this.client = require('https');
console.log('new client login');
this.on('error', function () {
console.log('an error occured in clientlogin');
});
};
GoogleClientLogin.prototype = new events.EventEmitter();
/**
* Logs in the user
* @method login
*/
GoogleClientLogin.prototype.login = function() {
var clientLogin = this;
var content = 'accountType=HOSTED_OR_GOOGLE'
+ '&Email=' + this.conf.email
+ '&Passwd=' + this.conf.password
+ '&service=cp'
+ '&source=' + userAgent + '_' + version;
var request = this.client.request(
{
host: 'www.google.com',
port: 443,
path: loginURL,
method: 'POST',
headers: {
'Content-Length': content.length,
'Content-Type': 'application/x-www-form-urlencoded'
}
},
function(response) {
var resp = '';
response.on('data', function(chunk) {
resp += chunk;
});
response.on('error', function (e) {
console.log('error on request: ', e);
});
response.on('end', function() {
var statusCode = response.statusCode;
if(statusCode >= 200 && statusCode < 300) {
(resp).split('\n').forEach(function(dataStr) {
var data = dataStr.split('=');
clientLogin.auths[data[0]] = data[1];
});
console.log('login success');
/**
* Fires when login was success
* @event login
*/
clientLogin.emit('login');
} else {
/**
* Fires when login was not success
* @event loginFailed
*/
console.log('client login failed', resp);
clientLogin.emit('loginFailed');
}
});
}
);
request.write(content);
request.end();
};
/**
* Returns the value of the Auth property
* @method getAuthId
*/
GoogleClientLogin.prototype.getAuthId = function() {
return this.auths.Auth;
};
/**
* Returns the value of the SID property
* @method getSID
*/
GoogleClientLogin.prototype.getSID = function() {
return this.auths.SID;
};
/**
* Returns the value of the LSID property
* @method getSID
*/
GoogleClientLogin.prototype.getLSID = function() {
return this.auths.LSID;
};
exports.GoogleClientLogin = GoogleClientLogin;
/*jslint indent: 2 */
/*global console: true, require: true, exports */
var EventEmitter = require('events').EventEmitter;
var GoogleClientLogin = require('./googleclientlogin').GoogleClientLogin;
var GoogleContacts = function (conf) {
var contacts = this;
this.conf = conf || {};
this.googleAuth = new GoogleClientLogin(this.conf);
this.googleAuth.on('error', function () {
console.error('an error occured on auth');
});
this.googleAuth.on('login', function () {
contacts.loggedIn = true;
});
this.client = require('https');
this.on('error', function () {
console.log('error occured in googlecontacts');
});
this.googleAuth.login();
};
GoogleContacts.prototype = new EventEmitter();
GoogleContacts.prototype.getContacts = function () {
var contacts = this;
if (!contacts.loggedIn) {
this.googleAuth.on('login', function () {
contacts._getContacts();
});
this.googleAuth.on('loginFailed', function () {
console.log('login failed', this.conf);
});
} else {
contacts._getContacts();
}
}
GoogleContacts.prototype._getContacts = function () {
var contacts = this, request;
request = this.client.request(
{
host: 'www.google.com',
port: 443,
path: '/m8/feeds/contacts/default/full?max-results=3000&alt=json',
method: 'GET',
headers: {
'Authorization': 'GoogleLogin auth=' + this.googleAuth.getAuthId(),
'Content-Type': 'application/x-www-form-urlencoded'
}
},
function (response) {
var resp = '';
console.log('get contacts status: ', response.statusCode);
response.on('data', function (data) {
resp += data;
});
response.on('error', function (e) {
console.error('an error occured getting contacts', e);
});
response.on('end', function () {
console.log('response end');
/*
if(response.statusCode >= 200 && response.statusCode < 300) {
contacts.contacts = JSON.parse(resp);
contacts.emit('contactsReceived');
} else {
console.log('data: ' + resp);
}
*/
});
response.socket.on('close', function () {
console.log('socket closed');
});
}
);
request.end();
};
exports.GoogleContacts = GoogleContacts;
var GoogleContacts = require('./googlecontacts').GoogleContacts;
var c = new GoogleContacts({
email: 'you@gmail.com',
password: 'password'
});
c.on('error', function () {
console.log('error');
});
c.on('contactsReceived', function () {
console.log(c.contacts);
});
c.getContacts();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment