Created
June 12, 2012 05:28
-
-
Save soundTricker/2915328 to your computer and use it in GitHub Desktop.
Google Apps 2 legged OAuth of Google Calendar API with Google Apps Script. Please import oauth.js and sha1.js from http://code.google.com/p/oauth/. if you happen any problem,Please modify oauth.js to below oauth.js code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function twoleggedOAuth() { | |
var action = "https://www.google.com/calendar/feeds/default/private/full?xoauth_requestor_id={user mail address}"; | |
var accessor = { consumerSecret: '{consumerSecret}' }; | |
var message = { method: 'get', action: action, parameters: {}, }; | |
OAuth.setParameter(message, 'oauth_version', '1.0'); | |
OAuth.setParameter(message, 'oauth_nonce', OAuth.nonce(32)); | |
OAuth.setParameter(message, 'oauth_timestamp', OAuth.timestamp()); | |
OAuth.setParameter(message, 'oauth_consumer_key', '{your domain}'); | |
OAuth.SignatureMethod.sign(message, accessor); | |
var val = OAuth.getAuthorizationHeader(null,message.parameters); | |
var option = | |
{"method": message.method, | |
headers : {"Authorization" : val,"GData-Version" : "2.0"} | |
}; | |
//Send Request | |
try { | |
var res = UrlFetchApp.fetch(action, option); | |
} catch(e) { | |
e.message.match(/&gsessionid=(.+) に/); //get gsessionid,But it's japanese locale. if you need any another local,please hack error responce. | |
var gsessionid = RegExp.$1; | |
var m2 = { method: 'get', action: action, parameters: {"gsessionid": gsessionid}}; | |
OAuth.setParameter(m2, 'oauth_version', '1.0'); | |
OAuth.setParameter(m2, 'oauth_nonce', OAuth.nonce(32)); | |
OAuth.setParameter(m2, 'oauth_timestamp', OAuth.timestamp()); | |
OAuth.setParameter(m2, 'oauth_consumer_key', '{your domain}'); | |
OAuth.SignatureMethod.sign(m2, accessor); | |
var val2 = OAuth.getAuthorizationHeader(null,m2.parameters); | |
action += "&gsessionid=" + gsessionid; | |
Logger.log(val2); | |
Logger.log(action); | |
option = { | |
"method": m2.method, | |
headers : {"Authorization" : val2,"GData-Version" : "2.0"} | |
}; | |
res = UrlFetchApp.fetch(action , option); | |
} | |
//display | |
var test = res.getContentText("UTF-8"); | |
Logger.log(test); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//on oauth.js OAuth class. | |
getAuthorizationHeader: function getAuthorizationHeader(realm, parameters) { | |
var header = 'OAuth '; | |
if(realm != null) header += 'realm="' + OAuth.percentEncode(realm) + '"'; | |
var list = OAuth.getParameterList(parameters); | |
for (var p = 0; p < list.length; ++p) { | |
var parameter = list[p]; | |
var name = parameter[0]; | |
if (name.indexOf("oauth_") == 0) { | |
header += OAuth.percentEncode(name) + '="' + OAuth.percentEncode(parameter[1]) + '"'; | |
if(p < list.length -1) { | |
header += ', '; | |
} | |
} | |
} | |
return header; | |
} | |
// | |
OAuth.nonce.CHARS = "0123456789abcdef"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment