Skip to content

Instantly share code, notes, and snippets.

@Swind
Created April 26, 2011 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swind/941970 to your computer and use it in GitHub Desktop.
Save Swind/941970 to your computer and use it in GitHub Desktop.
用Java取得Google Tasks資訊(1) - Example 1
val TAKSKS_URL = "https://mail.google.com/tasks/r/d"
def getTasksQuery(authToken:String)={
//好用又邪惡的隱式轉換
implicit def tuplesToNameValuePair(s:Tuple2[String,String]) = new BasicNameValuePair(s._1,s._2)
val actionID = 1
val post = new HttpPost(TAKSKS_URL)
post.addHeader("AT","1")
post.addHeader("Cookie","GTL="+authToken)
val request = """
{"action_list":[
{
"action_type":"get_all",
"action_id":%d,
"get_deleted":true
}
],
"client_version":0,
"latest_sync_point":0
}
""" format (actionID)
val queryList = Arrays.asList[BasicNameValuePair](
("r",request)
)
post.setEntity(new UrlEncodedFormEntity(queryList))
val response = httpClient.execute(post)
response.getStatusLine.getStatusCode match {
case 200=>
val reader = new BufferedReader(new InputStreamReader(response.getEntity.getContent))
printAll(reader)
case _=>
println(response.toString)
println(response.getStatusLine.getReasonPhrase)
println("httpRequest:"+response.getStatusLine.getStatusCode)
}
}
private def printAll(reader:BufferedReader):Unit={
val line = reader.readLine
if(line != null)
{
println(line)
printAll(reader)
}
}
val ClientLogURL = "https://www.google.com/accounts/ClientLogin"
val serviceType = "goanna_mobile"
def getAuthToken(account:String,password:String)={
//好用又邪惡的隱式轉換
implicit def tuplesToNameValuePair(s:Tuple2[String,String]) = new BasicNameValuePair(s._1,s._2)
val queryList = Arrays.asList[BasicNameValuePair](
("accountType","HOSTED_OR_GOOGLE"),
("Email",account),
("Passwd",password),
("service",serviceType),
("source","companyName-applicationName-versionID")
)
val post = new HttpPost(ClientLogURL)
post.setEntity(new UrlEncodedFormEntity(queryList))
val response = httpClient.execute(post)
//Get the response
def getToken(lines:BufferedReader):String={
val line = lines.readLine
println(line)
if(line != null && !line.startsWith("Auth="))
{
getToken(lines)
}
else
{
val results = line.split("=")
results(1)
}
}
response.getStatusLine.getStatusCode match {
case 200=>
val reader = new BufferedReader(new InputStreamReader(response.getEntity.getContent))
val token = getToken(reader)
reader.close
token
case _=>
response.toString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment