String accessToken = requestParams.get("access_token");
Integer expires = null;
if (requestParams.get(Constants.EXPIRES) != null) {
expires = new Integer(requestParams.get(Constants.EXPIRES));
}
accessGrant = new AccessGrant();
accessGrant.setKey(accessToken);
accessGrant.setAttribute(Constants.EXPIRES, expires);
if (permission != null) {
accessGrant.setPermission(permission);
} else {
accessGrant.setPermission(Permission.ALL);
}
accessGrant.setProviderId(providerId);
LOG.debug(accessGrant);
return accessGrant;
}
if (!providerState) {
throw new ProviderStateException();
}
String code = requestParams.get("code");
if (code == null || code.length() == 0) {
throw new SocialAuthException("Verification code is null");
}
LOG.debug("Verification Code : " + code);
String acode;
String accessToken = null;
try {
acode = URLEncoder.encode(code, "UTF-8");
} catch (Exception e) {
acode = code;
}
StringBuffer sb = new StringBuffer();
if (MethodType.GET.toString().equals(methodType)) {
sb.append(endpoints.get(Constants.OAUTH_ACCESS_TOKEN_URL));
char separator = endpoints.get(Constants.OAUTH_ACCESS_TOKEN_URL)
.indexOf('?') == -1 ? '?' : '&';
sb.append(separator);
}
sb.append("client_id=").append(oauth.getConfig().get_consumerKey());
sb.append("&redirect_uri=").append(this.successUrl);
sb.append("&client_secret=").append(
oauth.getConfig().get_consumerSecret());
sb.append("&code=").append(acode);
sb.append("&grant_type=authorization_code");
Response response;
String authURL = null;
try {
if (MethodType.GET.toString().equals(methodType)) {
authURL = sb.toString();
LOG.debug("URL for Access Token request : " + authURL);
response = HttpUtil.doHttpRequest(authURL, methodType, null,
null);
} else {
authURL = endpoints.get(Constants.OAUTH_ACCESS_TOKEN_URL);
LOG.debug("URL for Access Token request : " + authURL);
response = HttpUtil.doHttpRequest(authURL, methodType,
sb.toString(), null);
}
} catch (Exception e) {
throw new SocialAuthException("Error in url : " + authURL, e);
}
String result;
try {
result = response.getResponseBodyAsString(Constants.ENCODING);
} catch (IOException io) {
throw new SocialAuthException(io);
}
Map<String, Object> attributes = new HashMap<String, Object>();
Integer expires = null;
if (result.indexOf("{") < 0) {
String[] pairs = result.split("&");
for (String pair : pairs) {
String[] kv = pair.split("=");
if (kv.length != 2) {
throw new SocialAuthException(
"Unexpected auth response from " + authURL);
} else {
if (kv[0].equals("access_token")) {
accessToken = kv[1];
} else if (kv[0].equals("expires")) {
expires = Integer.valueOf(kv[1]);
} else if (kv[0].equals("expires_in")) {
expires = Integer.valueOf(kv[1]);
} else {
attributes.put(kv[0], kv[1]);
}
}
}
} else {
try {
JSONObject jObj = new JSONObject(result);
if (jObj.has("access_token")) {
accessToken = jObj.getString("access_token");
}
if (jObj.has("expires_in")) {
String str = jObj.getString("expires_in");
if (str != null && str.length() > 0) {
expires = Integer.valueOf(str);
}
}
if (accessToken != null) {
Iterator<String> keyItr = jObj.keys();
while (keyItr.hasNext()) {
String key = keyItr.next();
if (!"access_token".equals(key)
&& !"expires_in".equals(key)) {
attributes.put(key, jObj.optString(key));
}
}
}
} catch (JSONException je) {
throw new SocialAuthException("Unexpected auth response from "
+ authURL);
}
}
LOG.debug("Access Token : " + accessToken);
LOG.debug("Expires : " + expires);
if (accessToken != null) {
accessGrant = new AccessGrant();
accessGrant.setKey(accessToken);
accessGrant.setAttribute(Constants.EXPIRES, expires);
if (attributes.size() > 0) {
accessGrant.setAttributes(attributes);
}