Java LinkedIn OAuth2 get Profile

private String getProfile(String code, String accessToken) throws Exception {
        StringBuilder sb = new StringBuilder();
        try {

            LOGGER.info("get LinkedIn Profile---code---" + code);

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet getRequest = new HttpGet(
                    "https://api.linkedin.com/v1/people/~:(id,num-connections,picture-url,email-address,first-name,last-name)?oauth2_access_token="
                            + accessToken + "&format=json");
            getRequest.addHeader("oauth2_access_token", code);
            HttpResponse response = httpClient.execute(getRequest);

            if (response.getStatusLine().getStatusCode() != 200) {
                  throw new Exception("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            String output;

            while ((output = br.readLine()) != null) {
                sb.append(output);
            }
            br.close();
            LOGGER.info("JSON Object" + sb);

        } catch (IOException e) {
            LOGGER.error("Error in getLinkedInProfile()", e);
        }
        return sb.length() > 0 ? JsonUtils.getJsonString(JSONValue.parse(sb.toString())) : null;
    }

No comments:

Post a Comment