Map<String, Object> auth = JSON.parseMap(res.getBody());
public String extractEmail(OAuthResourceResponse oAuthResponse) throws Exception{ JSONObject body = (JSONObject) new JSONParser().parse(oAuthResponse.getBody()); String email = (String) body.get("email"); return email; } public String extractEmail(OAuthAccessTokenResponse oAuthResponse) throws Exception{
public <T> T getResourceObj(Class<T> resourceClass) { return EcJSONUtils.parseObject(super.getBody(), resourceClass); }
OAuthClientRequest request = OAuthClientRequest .tokenLocation("https://bitbucket.org/site/oauth2/access_token") .setGrantType(GrantType.PASSWORD) .setUsername("someUsernameEnteredByEndUser") .setPassword("somePasswordEnteredByEndUser") .buildBodyMessage(); String key = "yourConsumerKey"; String secret = "yourConsumerSecret"; byte[] unencodedConsumerAuth = (key + ":" + secret).getBytes(StandardCharsets.UTF_8); byte[] encodedConsumerAuth = Base64.getEncoder().encode(unencodedConsumerAuth); request.setHeader("Authorization", "Basic " + new String(encodedConsumerAuth, StandardCharsets.UTF_8)); OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient()); OAuthResourceResponse response = oAuthClient.resource(request, OAuth.HttpMethod.POST, OAuthResourceResponse.class); System.out.println("response body: " + response.getBody());
@Override public Optional<String> getUniqueUserId(final AccessTokenResponse accessTokenResponse) { String accessToken = accessTokenResponse.getAccessToken(); OAuthResourceResponse resourceResponse; try { OAuthClientRequest resourceRequest = new OAuthBearerClientRequest(userInformationRequestURL) .setAccessToken(accessToken) .buildHeaderMessage(); OAuthClient client = new OAuthClient(new URLConnectionClient()); resourceResponse = client.resource(resourceRequest, "GET", OAuthResourceResponse.class); } catch (OAuthSystemException | OAuthProblemException e) { LOGGER.error("Authentication failed.", e); return Optional.empty(); } JsonObject fromJson = new Gson().fromJson(resourceResponse.getBody(), JsonObject.class); return Optional.of(fromJson.get("id").toString()); }
String resourceResponseBody = resourceResponse.getBody(); log.trace(" OAuthResourceResponse, body{}" + resourceResponseBody);
AuthResponse authResponse; try{ authResponse = mapper.readValue(resp.getBody(), AuthResponse.class); }catch (Exception e){ return null; resp = oAuthClient.resource(request, OAuth.HttpMethod.GET, OAuthResourceResponse.class); Map<String,String> profile = mapper.readValue(resp.getBody(), Map.class); return authResponse;