-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathRennClient.java
More file actions
394 lines (363 loc) · 11.8 KB
/
RennClient.java
File metadata and controls
394 lines (363 loc) · 11.8 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
* @(#)RennClient.java, Jan 21, 2013.
*
* Copyright 2012 RenRen, Inc. All rights reserved.
*/
package com.renren.api;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
import com.renren.api.AccessToken.Type;
import com.renren.api.http.HttpRequest;
import com.renren.api.http.HttpRequest.HttpRequestException;
import com.renren.api.json.JSONException;
import com.renren.api.json.JSONObject;
import com.renren.api.mapper.JsonMappingException;
import com.renren.api.mapper.ObjectMapper;
import com.renren.api.service.AppService;
import com.renren.api.service.StatusService;
import com.renren.api.service.LocationService;
import com.renren.api.service.AlbumService;
import com.renren.api.service.BlogService;
import com.renren.api.service.LikeService;
import com.renren.api.service.PhotoService;
import com.renren.api.service.ShareService;
import com.renren.api.service.NotificationService;
import com.renren.api.service.UbbService;
import com.renren.api.service.FeedService;
import com.renren.api.service.PlaceService;
import com.renren.api.service.CommentService;
import com.renren.api.service.User;
import com.renren.api.service.UserService;
import com.renren.api.service.FriendService;
import com.renren.api.service.ProfileService;
/**
* @author Xun Dai <xun.dai@renren-inc.com>
*
*/
public class RennClient {
/**
* 客户端ID。应用的App ID。
*/
private String clientId;
/**
* 客户端密钥。应用的Secret Key。
*/
private String clientSecret;
/**
* Access Token
*/
private AccessToken accessToken;
/**
* API执行器
*/
private RennExecutor executor;
/**
* JSON到对象映射策略对象
*/
private ObjectMapper objectMapper;
/**
* app 对应的API服务
*/
private AppService appService;
/**
* status 对应的API服务
*/
private StatusService statusService;
/**
* location 对应的API服务
*/
private LocationService locationService;
/**
* album 对应的API服务
*/
private AlbumService albumService;
/**
* blog 对应的API服务
*/
private BlogService blogService;
/**
* like 对应的API服务
*/
private LikeService likeService;
/**
* photo 对应的API服务
*/
private PhotoService photoService;
/**
* share 对应的API服务
*/
private ShareService shareService;
/**
* notification 对应的API服务
*/
private NotificationService notificationService;
/**
* ubb 对应的API服务
*/
private UbbService ubbService;
/**
* feed 对应的API服务
*/
private FeedService feedService;
/**
* place 对应的API服务
*/
private PlaceService placeService;
/**
* comment 对应的API服务
*/
private CommentService commentService;
/**
* user 对应的API服务
*/
private UserService userService;
/**
* friend 对应的API服务
*/
private FriendService friendService;
/**
* profile 对应的API服务
*/
private ProfileService profileService;
/**
* @param clientId
* @param clientSecret
*/
public RennClient(String clientId, String clientSecret) {
super();
this.clientId = clientId;
this.clientSecret = clientSecret;
this.executor = new DefaultRennExecutor();
this.objectMapper = new ObjectMapper();
}
public AccessToken authorizeWithAuthorizationCode(String code, String redirectUri)
throws AuthorizationException {
Map<String, String> params = new HashMap<String, String>();
params.put("client_id", this.clientId);
params.put("client_secret", this.clientSecret);
params.put("grant_type", "authorization_code");
params.put("code", code);
params.put("redirect_uri", redirectUri);
params.put("token_type", AccessToken.Type.Bearer.toString());
this.accessToken = requestAccessToken(params);
return this.accessToken;
}
/**
*
* @param params
* @return
* @throws AuthorizationException
*/
private AccessToken requestAccessToken(Map<String, String> params)
throws AuthorizationException {
try {
HttpRequest request = HttpRequest.post("https://graph.renren.com/oauth/token");
request.form(params);
int statusCode = request.code();
String body = request.body();
JSONObject response = new JSONObject(body);
if (statusCode == HttpURLConnection.HTTP_OK && response.has("access_token")) {
String accessToken = response.getString("access_token");
String refreshToken = response.has("refresh_token") ? response
.getString("refresh_token") : null;
String macKey = response.has("mac_key") ? response.getString("mac_key") : null;
String macAlgorithm = response.has("mac_algorithm") ? response
.getString("mac_algorithm") : null;
AccessToken.Type type = response.has("mac_algorithm")
&& response.has("mac_algorithm") ? AccessToken.Type.MAC
: AccessToken.Type.Bearer;
int expiresIn = response.has("expires_in") ? response.getInt("expires_in"): 0;
User user = null;
if(response.has("user")){
try {
user = objectMapper.map(response.getJSONObject("user"), User.class);
} catch (JsonMappingException e) {
}
}
return new AccessToken(type, accessToken, refreshToken, macKey, macAlgorithm, expiresIn, user);
} else {
throw new AuthorizationException("Authorization failed with Authorization Code. "
+ response.getString("error") + ": "
+ response.getString("error_description"));
}
} catch (HttpRequestException e) {
throw new AuthorizationException("Authorization failed. Failed to send http request");
} catch (JSONException e) {
throw new AuthorizationException("Parse OAuth response failed. " + e.getMessage());
}
}
public void authorizeWithAccessToken(String bearerToken) throws AuthorizationException {
this.accessToken = new AccessToken(Type.Bearer, bearerToken, null, null, null);
}
public void authorizeWithAccessToken(String macToken, String macKey, String macAlgorithm)
throws AuthorizationException {
this.accessToken = new AccessToken(Type.MAC, macToken, null, macKey, macAlgorithm);
}
public void authorizeWithAccessToken(AccessToken token) throws AuthorizationException {
this.accessToken = token;
}
public void authorizeWithClientCredentials() throws AuthorizationException {
Map<String, String> params = new HashMap<String, String>();
params.put("client_id", this.clientId);
params.put("client_secret", this.clientSecret);
params.put("grant_type", "client_credentials");
params.put("token_type", AccessToken.Type.MAC.toString());
this.accessToken = requestAccessToken(params);
}
public void authorizeWithResourceOwnerPassword(String username, String password)
throws AuthorizationException {
Map<String, String> params = new HashMap<String, String>();
params.put("client_id", this.clientId);
params.put("client_secret", this.clientSecret);
params.put("grant_type", "password");
params.put("username", username);
params.put("password", password);
params.put("token_type", AccessToken.Type.MAC.toString());
this.accessToken = requestAccessToken(params);
}
/**
* @return the appService
*/
public AppService getAppService(){
if (appService == null) {
appService = new AppService(executor, accessToken, objectMapper);
}
return appService;
}
/**
* @return the statusService
*/
public StatusService getStatusService(){
if (statusService == null) {
statusService = new StatusService(executor, accessToken, objectMapper);
}
return statusService;
}
/**
* @return the locationService
*/
public LocationService getLocationService(){
if (locationService == null) {
locationService = new LocationService(executor, accessToken, objectMapper);
}
return locationService;
}
/**
* @return the albumService
*/
public AlbumService getAlbumService(){
if (albumService == null) {
albumService = new AlbumService(executor, accessToken, objectMapper);
}
return albumService;
}
/**
* @return the blogService
*/
public BlogService getBlogService(){
if (blogService == null) {
blogService = new BlogService(executor, accessToken, objectMapper);
}
return blogService;
}
/**
* @return the likeService
*/
public LikeService getLikeService(){
if (likeService == null) {
likeService = new LikeService(executor, accessToken, objectMapper);
}
return likeService;
}
/**
* @return the photoService
*/
public PhotoService getPhotoService(){
if (photoService == null) {
photoService = new PhotoService(executor, accessToken, objectMapper);
}
return photoService;
}
/**
* @return the shareService
*/
public ShareService getShareService(){
if (shareService == null) {
shareService = new ShareService(executor, accessToken, objectMapper);
}
return shareService;
}
/**
* @return the notificationService
*/
public NotificationService getNotificationService(){
if (notificationService == null) {
notificationService = new NotificationService(executor, accessToken, objectMapper);
}
return notificationService;
}
/**
* @return the ubbService
*/
public UbbService getUbbService(){
if (ubbService == null) {
ubbService = new UbbService(executor, accessToken, objectMapper);
}
return ubbService;
}
/**
* @return the feedService
*/
public FeedService getFeedService(){
if (feedService == null) {
feedService = new FeedService(executor, accessToken, objectMapper);
}
return feedService;
}
/**
* @return the placeService
*/
public PlaceService getPlaceService(){
if (placeService == null) {
placeService = new PlaceService(executor, accessToken, objectMapper);
}
return placeService;
}
/**
* @return the commentService
*/
public CommentService getCommentService(){
if (commentService == null) {
commentService = new CommentService(executor, accessToken, objectMapper);
}
return commentService;
}
/**
* @return the userService
*/
public UserService getUserService(){
if (userService == null) {
userService = new UserService(executor, accessToken, objectMapper);
}
return userService;
}
/**
* @return the friendService
*/
public FriendService getFriendService(){
if (friendService == null) {
friendService = new FriendService(executor, accessToken, objectMapper);
}
return friendService;
}
/**
* @return the profileService
*/
public ProfileService getProfileService(){
if (profileService == null) {
profileService = new ProfileService(executor, accessToken, objectMapper);
}
return profileService;
}
}