-
Notifications
You must be signed in to change notification settings - Fork 662
Full working example for interacting with google
Franklin van de Meent edited this page Jul 29, 2014
·
3 revisions
var sys = require('util');
var OAuth = require('oauth').OAuth;
var oa = new OAuth("https://www.google.com/accounts/OAuthGetRequestToken",
"https://www.google.com/accounts/OAuthGetAccessToken",
"anonymous", "anonymous",
"1.0A", undefined, "HMAC-SHA1");
oa.getOAuthRequestToken({"scope":"http://www.google.com/calendar/feeds"}, function(error, oauth_token, oauth_token_secret, results) {
if (error) {
console.log('error: ' + JSON.stringify(error));
} else {
console.log('oauth_token: ' + oauth_token);
console.log('oauth_token_secret: ' + oauth_token_secret);
console.log('requestoken results: ' + sys.inspect(results));
console.log("Requesting access token");
console.log('Please go to https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=' + oauth_token);
ask("Please enter the verification code:\n", /[\w\d]+/, function(data) {
oa.getOAuthAccessToken(oauth_token, oauth_token_secret, data, accessTokenCallback);
});
}
});
function accessTokenCallback(error, access_token, access_token_secret, results) {
if (error) {
console.log('error: ' + JSON.stringify(error));
} else {
console.log('oauth_access_token: ' + access_token);
console.log('oauth_access_token_secret: ' + access_token_secret);
console.log('accesstoken results: ' + sys.inspect(results));
console.log('getting a list of all your calendars');
var url = 'http://www.google.com/calendar/feeds/default/allcalendars/full?v=2&alt=jsonc';
var request = oa.get(url, access_token, access_token_secret, function(error, data) {
if (error) {
console.log(error);
} else {
var calendars = JSON.parse(data).data.items;
for (var i = 0; i < calendars.length; ++i) {
console.log(calendars[i].title);
}
}
});
}
}
function ask(question, format, callback) {
var stdin = process.stdin;
var stdout = process.stdout;
stdin.resume();
stdout.write(question);
stdin.once('data', function(data) {
data = data.toString().trim();
if (format.test(data)) {
callback(data);
} else {
stdout.write("It should match: "+ format +"\n");
ask(question, format, callback);
}
});
}