Created
January 2, 2011 07:07
-
-
Save withgod/762367 to your computer and use it in GitHub Desktop.
commons-httpclient3 を使ったgoogle calendar新規作成の実装
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
package withgod; | |
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; | |
import org.apache.commons.httpclient.HttpClient; | |
import org.apache.commons.httpclient.methods.PostMethod; | |
import org.apache.commons.httpclient.methods.StringRequestEntity; | |
import org.apache.commons.httpclient.params.HttpClientParams; | |
import org.apache.commons.httpclient.params.HttpMethodParams; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class Fizz { | |
public static void main(String[] args) throws Exception { | |
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); | |
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); | |
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug"); | |
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug"); | |
HttpClient client = new HttpClient(); | |
PostMethod authReq = new PostMethod("https://www.google.com/accounts/ClientLogin"); | |
HttpClientParams params = client.getParams(); | |
params.setBooleanParameter(HttpClientParams.REJECT_RELATIVE_REDIRECT, false); //適当に設定してみた | |
params.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, false); | |
params.setIntParameter(HttpClientParams.MAX_REDIRECTS, 10); | |
authReq.addParameter("accountType", "GOOGLE"); | |
authReq.addParameter("Email", "[email protected]"); | |
authReq.addParameter("Passwd", "password"); | |
authReq.addParameter("source", "withgod-sample-1"); | |
authReq.addParameter("service", "cl"); | |
client.executeMethod(authReq); | |
System.out.println(authReq.getStatusLine()); | |
byte[] responseBody = authReq.getResponseBody(); | |
String authRet = new String(responseBody); | |
String authToken = authRet.split("[\r\n]+")[2].split("=")[1]; | |
System.out.println("authToken[" + authToken + "]"); | |
authReq.releaseConnection(); | |
String reqStr = "<?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gCal='http://schemas.google.com/gCal/2005' xmlns:gd='http://schemas.google.com/g/2005'><title>Big League Schedule2</title><atom:summary xmlns:atom='http://www.w3.org/2005/Atom'>xx This calendar contains the practice schedule and game times.</atom:summary><gCal:timezone value='America/Los_Angeles'/><gCal:hidden value='false'/><gCal:color value='#2952A3'/><gd:where rel='' label='' valueString='Oakland'/></entry>"; | |
PostMethod postReq = new PostMethod("https://www.google.com/calendar/feeds/default/owncalendars/full"); | |
postReq.addRequestHeader("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"); | |
postReq.addRequestHeader("Pragma", "no-cache"); | |
postReq.addRequestHeader("Cache-Control", "no-cache"); | |
postReq.addRequestHeader("GData-Version", "2.0"); | |
postReq.addRequestHeader("User-Agent", "withgod-sample-1"); | |
postReq.addRequestHeader("Authorization", "GoogleLogin auth=" + authToken); | |
postReq.setRequestEntity(new StringRequestEntity(reqStr, "application/atom+xml", "UTF-8")); | |
postReq.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); | |
client.executeMethod(postReq); | |
System.out.println(postReq.getStatusLine()); | |
byte[] responseBody2 = postReq.getResponseBody(); | |
System.out.println(new String(responseBody2)); | |
//Location周りが上手く動いてくれないので、自分でレスポンス解析して再投稿してます | |
Matcher m = Pattern.compile("HREF=\"([^\"]+)").matcher(new String(responseBody2)); | |
if (m.find()) { | |
PostMethod postReq2 = new PostMethod(m.group(1)); | |
postReq2.addRequestHeader("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"); | |
postReq2.addRequestHeader("Pragma", "no-cache"); | |
postReq2.addRequestHeader("Cache-Control", "no-cache"); | |
postReq2.addRequestHeader("GData-Version", "2.0"); | |
postReq2.addRequestHeader("User-Agent", "withgod-sample-1"); | |
postReq2.addRequestHeader("Authorization", "GoogleLogin auth=" + authToken); | |
postReq2.setRequestEntity(new StringRequestEntity(reqStr, "application/atom+xml", "UTF-8")); | |
client.executeMethod(postReq2); | |
System.out.println(postReq2.getStatusLine()); | |
byte[] responseBody3 = postReq2.getResponseBody(); | |
System.out.println(new String(responseBody3)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment