Skip to content

Instantly share code, notes, and snippets.

@soundTricker
Created April 26, 2012 02:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save soundTricker/2495204 to your computer and use it in GitHub Desktop.
Save soundTricker/2495204 to your computer and use it in GitHub Desktop.
google apps scriptで直接Google Calendar API(v2)を呼ぶ(Call Google Calendar API with Google Apps Script in direct)
function myFunction() {
//oauth の設定
var oauth = UrlFetchApp.addOAuthService(Session.getActiveUser().getEmail());
oauth.setConsumerKey("anonymous"); //ちゃんとしたのを使ったほうがいいとおもふ
oauth.setConsumerSecret("anonymous"); //ちゃんとしたのを使ったほうがいいとおもふ
oauth.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=https%3A%2F%2Fwww.google.com%2Fcalendar%2Ffeeds%2F");
oauth.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oauth.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
//リクエストオプションの設定
var options = {
"headers" : {"GData-Version": "2.0"},
"method" : "get",
"oAuthServiceName" : Session.getActiveUser().getEmail(),
"oAuthUseToken" : "always"
};
//urlの作成
var url = "https://www.google.com/calendar/feeds/"
+ "相手のメアド"
+ "/private/full?alt=json&gsessionid=";
//リクエストの発行
var res = null;
try {
res = UrlFetchApp.fetch(url, options);
} catch(e) {
//レスポンスコードが302の時はgsessionid取得して再度リクエストを投げる
if(e.message.indexOf(" 302 ") > 0) {
var gsessionid = e.message.match(/gsessionid=(.+)">/m)[1];
url += gsessionid;
res = UrlFetchApp.fetch(url, options);
} else {
throw e;
}
}
//表示
var test = res.getContentText("UTF-8");
Logger.log(test);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment