2015
25
Nov
GAE googleカレンダーから、祝日取得

APIキーの種類で、はまったので、メモ。。
指定期間の祝日をもってくるプログラム。
google calenderはv3を使用。。
●JAVAで取得する場合●
APIキーは、googleの管理画面から作成。
【メモ】
APIキーのタイプを「サーバー」で作成しないとダメ。
・ローカル環境で確認する場合は、
「リクエストを受け入れるサーバー IP アドレス」って欄に自分のIPを登録
・GAEサーバーにアップして確認する場合は、
「リクエストを受け入れるサーバー IP アドレス」って欄は空白。
String APIKEY = "ここにサーバーキー"; String jsondata; //基準日の1年前~1年後の期間の祝日を取得 public GoogleCalender(String targetDateText) throws IOException, JSONException{ //基準日の年を取得 int year = Integer.parseInt(targetDateText.substring(0,4)); //基準日から1年前 String start_date = String.valueOf(year-1); //基準日から1年後 String end_date = String.valueOf(year+1); String strurl = "https://www.googleapis.com/calendar/v3/calendars/japanese__ja%40holiday.calendar.google.com/events" + "?timeMax="+end_date+"-12-31T00%3A00%3A00Z" + "&timeMin="+start_date+"-01-01T00%3A00%3A00Z" + "&key="+APIKEY; URL url = new URL(strurl); HttpURLConnection con = (HttpURLConnection)url.openConnection(); con.connect(); con.getResponseCode(); byte bodyByte[] = new byte[100000]; InputStream in = con.getInputStream(); DataInputStream data = new DataInputStream(in); data.read(bodyByte); in.close(); this.jsondata = new String(bodyByte, "UTF-8"); }
●javascriptで取得する場合●
javascriptの場合は、APIキーのタイプを「ブラウザ」で作成しないとダメ。
管理画面の「この HTTP リファラー(ウェブサイト)からのリクエストを受け入れる」に
↓を設定。
*.appspot.com/*
