Java Code Examples for org.brickred.socialauth.Profile#setDisplayName()

The following examples show how to use org.brickred.socialauth.Profile#setDisplayName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GitHubImpl.java    From socialauth with MIT License 5 votes vote down vote up
private Profile getProfile() throws Exception {
	String presp;

	try {
		Response response = authenticationStrategy.executeFeed(ENDPOINTS.get(Constants.API_URL) + "/user");
		presp = response.getResponseBodyAsString(Constants.ENCODING);
	} catch (Exception e) {
		throw new SocialAuthException("Error while getting profile from "
				+ ENDPOINTS.get(Constants.API_URL) + "/user", e);
	}
	try {
		LOG.debug("User Profile : " + presp);
		JSONObject resp = new JSONObject(presp);
		Profile p = new Profile();
		p.setValidatedId(resp.optString("id", null));
		p.setFullName(resp.optString("name", null));
		p.setEmail(resp.optString("email", null));
		p.setLocation(resp.optString("location", null));
		p.setProfileImageURL(resp.optString("avatar_url", null));
		p.setDisplayName(resp.optString("login", null));
		p.setProviderId(getProviderId());
		if (config.isSaveRawResponse()) {
			p.setRawResponse(presp);
		}
		userProfile = p;
		return p;
	} catch (Exception ex) {
		throw new ServerDataException(
				"Failed to parse the user profile json : " + presp, ex);
	}
}
 
Example 2
Source File: InstagramImpl.java    From socialauth with MIT License 4 votes vote down vote up
private Profile getProfile() throws Exception {
	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", null));
		String full_name = data.optString("full_name", null);
		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", null));
		p.setProviderId(getProviderId());
		if (config.isSaveRawResponse()) {
			p.setRawResponse(respStr);
		}
		return p;
	} else {
		throw new SocialAuthException(
				"Failed to retrieve the user profile from " + PROFILE_URL
						+ ". Server response " + response.getStatus());
	}
}
 
Example 3
Source File: MySpaceImpl.java    From socialauth with MIT License 4 votes vote down vote up
private Profile getProfile() throws Exception {
	LOG.debug("Obtaining user profile");
	Profile profile = new Profile();

	Response serviceResponse = null;
	try {
		serviceResponse = authenticationStrategy.executeFeed(PROFILE_URL);
	} catch (Exception e) {
		throw new SocialAuthException(
				"Failed to retrieve the user profile from  " + PROFILE_URL,
				e);
	}
	if (serviceResponse.getStatus() != 200) {
		throw new SocialAuthException(
				"Failed to retrieve the user profile from  " + PROFILE_URL
						+ ". Staus :" + serviceResponse.getStatus());
	}

	String result;
	try {
		result = serviceResponse
				.getResponseBodyAsString(Constants.ENCODING);
		LOG.debug("User Profile :" + result);
	} catch (Exception exc) {
		throw new SocialAuthException("Failed to read response from  "
				+ PROFILE_URL, exc);
	}
	JSONObject pObj = new JSONObject();
	JSONObject jobj = new JSONObject(result);
	if (jobj.has("person")) {
		pObj = jobj.getJSONObject("person");
	} else {
		throw new ServerDataException(
				"Failed to parse the user profile json : " + result);
	}
	profile.setDisplayName(pObj.optString("displayName", null));
	profile.setValidatedId(pObj.optString("id", null));
	if (pObj.has("name")) {
		JSONObject nobj = pObj.getJSONObject("name");
		profile.setLastName(nobj.optString("familyName", null));
		profile.setFirstName(nobj.optString("givenName", null));
	}
	profile.setLocation(pObj.optString("location", null));
	profile.setDisplayName(pObj.optString("nickname", null));
	profile.setLanguage(pObj.optString("lang", null));
	profile.setProfileImageURL(pObj.optString("thumbnailUrl", null));
	profile.setProviderId(getProviderId());
	if (config.isSaveRawResponse()) {
		profile.setRawResponse(result);
	}
	userProfile = profile;
	return profile;
}
 
Example 4
Source File: StackExchangeImpl.java    From socialauth with MIT License 4 votes vote down vote up
private Profile getProfile() throws Exception {
	String presp;
	String profileURL = PROFILE_URL;
	String site = "stackoverflow";
	if (config.getCustomProperties() != null) {
		if (config.getCustomProperties().get("site") != null) {
			site = config.getCustomProperties().get("site");
		}
		profileURL += "?key=" + config.getCustomProperties().get("key")
				+ "&site=" + site;
	} else {
		throw new SocialAuthException(
				"Please set 'stackapps.com.custom.key' property in oauth_consumer.properties  ");
	}
	try {
		Response response = authenticationStrategy.executeFeed(profileURL);
		presp = response.getResponseBodyAsString(Constants.ENCODING);
	} catch (Exception e) {
		throw new SocialAuthException("Error while getting profile from "
				+ profileURL, e);
	}
	try {
		LOG.debug("User Profile : " + presp);
		JSONObject jsonResp = new JSONObject(presp);
		Profile p = new Profile();
		if (jsonResp.has("items")) {
			JSONArray items = jsonResp.getJSONArray("items");
			if (items.length() > 0) {
				JSONObject resp = items.getJSONObject(0);
				p.setDisplayName(resp.optString("display_name", null));
				p.setFullName(resp.optString("display_name", null));
				p.setProfileImageURL(resp.optString("profile_image", null));
				p.setValidatedId(resp.optString("user_id", null));
				p.setLocation(resp.optString("location", null));
				if (config.isSaveRawResponse()) {
					p.setRawResponse(presp);
				}
				p.setProviderId(getProviderId());
				if (config.isSaveRawResponse()) {
					p.setRawResponse(presp);
				}
			}
		}
		userProfile = p;
		return p;

	} catch (Exception ex) {
		throw new ServerDataException(
				"Failed to parse the user profile json : " + presp, ex);
	}
}