This repository was archived by the owner on Oct 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTestApi.java
More file actions
107 lines (90 loc) · 3.49 KB
/
Copy pathTestApi.java
File metadata and controls
107 lines (90 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import com.oDesk.api.*;
import com.oDesk.api.Routers.Organization.Users;
import com.oDesk.api.Routers.Mc;
import com.oDesk.api.Routers.Reports.Time;
import java.util.HashMap;
import java.util.Scanner;
import java.net.URLDecoder;
import org.json.JSONException;
import org.json.JSONObject;
public class TestApi {
@SuppressWarnings("unused")
public static void main(String[] args) {
//assign access token-secret pair if they are already known
//this process is up to application how to save and store
//in secure token's data
//String aToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
//String aSecret = "xxxxxxxxxxxxxx";
//by default token and secret are unknown
//and application must follow authorization process
String aToken = null;
String aSecret = null;
OAuthClient client = new OAuthClient(null);
// authorize application and get access token
if (aToken == null && aSecret == null) {
Scanner scanner = new Scanner(System.in);
String authzUrl = client.getAuthorizationUrl();
System.out.println(authzUrl);
System.out.println("1. Copy paste the following url in your browser : ");
System.out.println(authzUrl);
System.out.println("2. Grant access ");
System.out.println("3. Copy paste the oauth_verifier parameter here :");
String oauth_verifier = scanner.nextLine();
String verifier = null;
try {
verifier = URLDecoder.decode(oauth_verifier,"UTF-8");
}
catch (Exception e) {
e.printStackTrace();
}
HashMap<String, String> token = client.getAccessTokenSet(verifier);
scanner.close();
System.out.println(token);
} else {
// set known access token-secret pair
client.setTokenWithSecret(aToken, aSecret);
}
JSONObject json1 = null;
JSONObject json2 = null;
JSONObject json3 = null;
try {
// Get info of authenticated user
Users users = new Users(client);
json1 = users.getMyInfo();
// get my uid
String myId = null;
try {
JSONObject user = json1.getJSONObject("user");
myId = user.getString("id");
System.out.println(myId);
}
catch (JSONException e) {
e.printStackTrace();
}
// Get test report
//Build the list of parameters
HashMap<String, String> params2 = new HashMap<String, String>();
params2.put("tqx", "out:json");
params2.put("tq", "select task where worked_on >= '2014-06-01' AND worked_on <= '2014-06-03' order by worked_on");
Time report = new Time(client);
json2 = report.getByFreelancerLimited(myId, params2);
// post a new message
/*HashMap<String, String> params2 = new HashMap<String, String>();
params2.put("recipients", "invalid");
params2.put("body", "body of the message");
Mc mc = new Mc(client);
json2 = mc.startNewThread(myId, params2);*/
// mark thread as read
HashMap<String, String> params3 = new HashMap<String, String>();
params3.put("read", "true");
Mc mc = new Mc(client);
json3 = mc.markThread(myId, "8888888", params3);
}
catch (JSONException e) {
e.printStackTrace();
}
System.out.println(json1);
System.out.println(json2);
System.out.println(json3);
}
}