Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add return value for RennClient.authorizeWithAuthorizationCode #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/main/java/com/renren/api/AccessToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package com.renren.api;

import com.renren.api.service.User;

/**
* @author Xun Dai <[email protected]>
*
Expand All @@ -24,8 +26,28 @@ public static enum Type {
private String macKey;

private String macAlgorithm;

private int expiresIn;

private User user;

public User getUser() {
return user;
}

public Type getType() {
public void setUser(User user) {
this.user = user;
}

public int getExpiresIn() {
return expiresIn;
}

public void setExpiresIn(int expiresIn) {
this.expiresIn = expiresIn;
}

public Type getType() {
return type;
}

Expand All @@ -45,6 +67,18 @@ public String getMacAlgorithm() {
return macAlgorithm;
}

public AccessToken(Type type, String accessToken, String refreshToken, String macKey,
String macAlgorithm, int expiresIn, User user) {
super();
this.type = type;
this.accessToken = accessToken;
this.refreshToken = refreshToken;
this.macKey = macKey;
this.macAlgorithm = macAlgorithm;
this.expiresIn = expiresIn;
this.user = user;
}

public AccessToken(Type type, String accessToken, String refreshToken, String macKey,
String macAlgorithm) {
super();
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/com/renren/api/RennClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;
Expand All @@ -28,6 +29,7 @@
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;
Expand Down Expand Up @@ -141,7 +143,7 @@ public RennClient(String clientId, String clientSecret) {
this.objectMapper = new ObjectMapper();
}

public void authorizeWithAuthorizationCode(String code, String redirectUri)
public AccessToken authorizeWithAuthorizationCode(String code, String redirectUri)
throws AuthorizationException {

Map<String, String> params = new HashMap<String, String>();
Expand All @@ -150,9 +152,10 @@ public void authorizeWithAuthorizationCode(String code, String redirectUri)
params.put("grant_type", "authorization_code");
params.put("code", code);
params.put("redirect_uri", redirectUri);
params.put("token_type", AccessToken.Type.MAC.toString());
params.put("token_type", AccessToken.Type.Bearer.toString());

this.accessToken = requestAccessToken(params);
return this.accessToken;
}

/**
Expand Down Expand Up @@ -181,8 +184,18 @@ private AccessToken requestAccessToken(Map<String, String> params)
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);
return new AccessToken(type, accessToken, refreshToken, macKey, macAlgorithm, expiresIn, user);

} else {
throw new AuthorizationException("Authorization failed with Authorization Code. "
Expand Down