LOG.debug("Obtaining user profile");
Response response;
try {
response = authenticationStrategy.executeFeed(PROFILE_URL);
} catch (Exception e) {
throw new SocialAuthException(
"Failed to retrieve the user profile from " + PROFILE_URL,
e);
}
if (response.getStatus() == 200) {
String respStr = response
.getResponseBodyAsString(Constants.ENCODING);
LOG.debug("Profile JSON string :: " + respStr);
JSONObject obj = new JSONObject(respStr);
JSONObject data = obj.getJSONObject("data");
Profile p = new Profile();
p.setValidatedId(data.optString("id"));
String full_name = data.optString("full_name");
p.setDisplayName(full_name);
if (full_name != null) {
String[] names = full_name.split(" ");
if (names.length > 1) {
p.setFirstName(names[0]);
p.setLastName(names[1]);
} else {
p.setFirstName(full_name);
}
}
p.setProfileImageURL(data.optString("profile_picture"));
p.setProviderId(getProviderId());
return p;
} else {
throw new SocialAuthException(
"Failed to retrieve the user profile from " + PROFILE_URL
+ ". Server response " + response.getStatus());
}
}