@Override public Identity identity() throws IOException { Identity identity = Identity.ANONYMOUS; if (this.resource.uriInfo().getQueryParameters() .containsKey(LinkedIn.FLAG)) { final List<String> code = this.resource.uriInfo() // @checkstyle MultipleStringLiterals (1 line) .getQueryParameters().get("code"); if (code == null || code.isEmpty()) { throw new WebApplicationException( new IllegalArgumentException( "HTTP query parameter 'code' is mandatory" ), HttpURLConnection.HTTP_BAD_REQUEST ); } identity = this.fetch(this.token(code.get(0))); } return identity; }
@Override public Link link() { return new Link( "rexsl:linkedin", UriBuilder .fromUri("https://www.linkedin.com/uas/oauth2/authorization") // @checkstyle MultipleStringLiterals (2 lines) .queryParam("client_id", "{client_id}") .queryParam("redirect_uri", "{redirect_uri}") .queryParam("state", "{state}") .build(this.app, this.redirectUri(), LinkedIn.STATE) ); }
/** * Get user name from LinkedIn, with the token provided. * @param token LinkedIn access token * @return The user found in LinkedIn * @throws java.io.IOException If fails */ private Identity fetch(final String token) throws IOException { final URI uri = UriBuilder // @checkstyle LineLength (1 line) .fromUri("https://api.linkedin.com/v1/people/~:(id,first-name,last-name,picture-url)?format=json") .queryParam("oauth2_access_token", "{token}") .build(token); return this.parse( new JdkRequest(uri) .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON) .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class).json().readObject() ); }