Last active
December 13, 2015 17:58
-
-
Save s-aska/4951559 to your computer and use it in GitHub Desktop.
7kai Tasks OAuth sample code for Java.
参考: http://blog.unfindable.net/archives/788
依存:
- http://commons.apache.org/codec/
- http://code.google.com/p/oauth-signpost/downloads/list
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 doublespark; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
import java.util.Map; | |
// http://code.google.com/p/oauth-signpost/ | |
import oauth.signpost.OAuthConsumer; | |
import oauth.signpost.basic.DefaultOAuthConsumer; | |
// http://code.google.com/p/google-gson/ | |
import com.google.gson.Gson; | |
import com.google.gson.stream.*; | |
public class Me { | |
public static void main(String[] args) throws Exception { | |
OAuthConsumer consumer = new DefaultOAuthConsumer( | |
"YOUR_APP_CONSUMER_KEY", "YOUR_APP_CONSUMER_SECRET"); | |
consumer.setTokenWithSecret("YOUR_ACCESS_TOKEN", "YOUR_ACCESS_SECRET"); | |
URL url = new URL("https://tasks.7kai.org/api/1/account/me"); | |
HttpURLConnection api_request = (HttpURLConnection) url | |
.openConnection(); | |
consumer.sign(api_request); | |
api_request.connect(); | |
InputStream in = api_request.getInputStream(); | |
JsonReader jsr = new JsonReader(new InputStreamReader(in, "utf-8")); | |
Gson gson = new Gson(); | |
MeResponse res = new MeResponse(); | |
res = gson.fromJson( jsr, res.getClass() ); | |
Account account = res.account; | |
List<SubAccount> sub_accounts = res.sub_accounts; | |
Map<String, Account> users = res.users; | |
List<TaskList> lists = res.lists; | |
System.out.println( account.name ); | |
System.out.println( res.holidays ); | |
for (SubAccount sub_account: sub_accounts) { | |
System.out.println( sub_account.name ); | |
System.out.println( sub_account.code ); | |
} | |
for (TaskList list: lists) { | |
System.out.println( "===== " + list.name + " =====" ); | |
System.out.println( "Owner: " + users.get(list.owner).name ); | |
for (Account member: list.getMembers(users)) { | |
System.out.println( "Member: " + member.name ); | |
} | |
for (Task task: list.tasks) { | |
System.out.println( "[" + list.name + "] " + task.name + " " + task.getStatusName() ); | |
for (TaskAction action: task.actions) { | |
Account user = users.get(action.account_id); | |
if (action.action.equals("comment")) { | |
System.out.println( "- " + action.message + " by " + user.name + " at " + action.getTime() ); | |
} else { | |
System.out.println( "- " + action.action + " by " + user.name + " at " + action.getTime() ); | |
} | |
} | |
} | |
} | |
} | |
public static class MeResponse { | |
int success; | |
Account account; | |
String list_ids; | |
String modified_on; | |
Sign sign; | |
Map<String, String> holidays; | |
Map<String, Account> users; | |
List<SubAccount> sub_accounts; | |
List<TaskList> lists; | |
public MeResponse() { | |
} | |
} | |
public static class Account { | |
String name; | |
String icon; | |
} | |
public static class Sign { | |
String account_id; | |
String name; | |
String icon; | |
} | |
public static class SubAccount { | |
String code; | |
String name; | |
String created_on; | |
String updated_on; | |
String authenticated_on; | |
SubAccountData data; | |
} | |
public static class SubAccountData { | |
String icon; | |
} | |
public static class TaskList { | |
String id; | |
String name; | |
String description; | |
int original; | |
String owner; | |
List<String> members; | |
String invite_code; | |
String public_code; | |
List<Task> tasks; | |
String last_task_id; | |
long actioned_on; | |
public List<Account> getMembers(Map<String, Account> users) { | |
List<Account> rows = new ArrayList<Account>(); | |
for (String account_id: this.members) { | |
rows.add(users.get(account_id)); | |
} | |
return rows; | |
} | |
public Date getActionedOn() { | |
return new Date(this.actioned_on); | |
} | |
} | |
public static class Task { | |
String id; | |
String parent_id; | |
String name; | |
String due; | |
String duration; | |
String status = "0"; | |
String pending = "0"; | |
String closed = "0"; | |
String requester; | |
String registrant; | |
List<String> assign; | |
List<TaskAction> actions; | |
String last_comment_id; | |
long created_on; | |
long updated_on; | |
public String getStatusName() { | |
if (this.closed.equals("1")) { | |
return "closed"; | |
} else if (this.pending.equals("1")) { | |
return "pending"; | |
} else if (this.status.equals("1")) { | |
return "start"; | |
} else if (this.status.equals("2")) { | |
return "fix"; | |
} else { | |
return "open"; | |
} | |
} | |
public Date getCreatedOn() { | |
return new Date(this.created_on); | |
} | |
public Date getUpdatedOn() { | |
return new Date(this.updated_on); | |
} | |
} | |
public static class TaskAction { | |
String id; | |
String account_id; | |
String action; | |
String message; | |
long time; | |
public Date getTime() { | |
return new Date(this.time); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment