service = new OAuth20Service(api, oauthConfig);
@Override public void signRequest(String accessToken, OAuthRequest request) { super.signRequest(accessToken, request); final Mac mac; try { mac = Mac.getInstance("HmacSHA256"); final SecretKeySpec secretKey = new SecretKeySpec(getApiSecret().getBytes(), "HmacSHA256"); mac.init(secretKey); final Formatter appsecretProof = new Formatter(); for (byte b : mac.doFinal(accessToken.getBytes())) { appsecretProof.format("%02x", b); } request.addParameter("appsecret_proof", appsecretProof.toString()); } catch (NoSuchAlgorithmException | InvalidKeyException e) { throw new IllegalStateException("There is a problem while generating Facebook appsecret_proof.", e); } } }
authorizationUrl = oauth20Service.getAuthorizationUrl(additionalParams); } else { authorizationUrl = oauth20Service.getAuthorizationUrl();
@Override public void onAuthCodeReceived(String authCode) { authCallbackHandler.setListener(null); try { String contentJson = json.toJson(new CreateIssueBody(title, body)); OAuth2AccessToken accessToken = apiService.getAccessToken(authCode); OAuthRequest request = new OAuthRequest(Verb.POST, "https://api.github.com/repos/"+GITHUB_OWNER+"/"+GITHUB_REPO+"/issues"); request.setPayload(contentJson); apiService.signRequest(accessToken, request); Response response = apiService.execute(request); if (response.getCode() != 201) { resultHandler.onError(new IllegalStateException("GitHub returned bad code: " + response.getCode() + "\n" + response.getMessage() + "\n" + response.getBody())); } else { JsonValue jsonRoot = new JsonReader().parse(response.getBody()); String issueUrl = jsonRoot.getString("html_url"); resultHandler.onSuccess(issueUrl); } } catch (IOException | InterruptedException | ExecutionException | OAuthException e) { e.printStackTrace(); resultHandler.onError(e); } } });
@Override protected OAuth2AccessToken doInBackground(Void... params) { try { final OAuth2AccessToken accessToken = (OAuth2AccessToken) oauth20Service.getAccessToken(authorizationCode); return accessToken; } catch (OAuthConnectionException ex) { Log.e(TAG, "OAuth connection exception: " + ex.getMessage()); ex.printStackTrace(); return null; } catch (IOException ex) { Log.e(TAG, "An exception occurred getRequestToken: " + ex.getMessage()); ex.printStackTrace(); return null; } }
@Override public Response execute(OAuthRequest request) throws InterruptedException, ExecutionException, IOException { OAuthRequest authRequest = addClientAuthentication(request); return super.execute(authRequest); }
@Override public void signRequest(String accessToken, OAuthRequest request) { super.signRequest(accessToken, request); request.addHeader("X-Client-ID", getApiKey()); } }
Sys.openURL(apiService.getAuthorizationUrl());
@Override protected void retrieveAccessToken(final WebContext context, final OAuthCredentials credentials) { OAuth20Credentials oAuth20Credentials = (OAuth20Credentials) credentials; // no request token saved in context and no token (OAuth v2.0) final String code = oAuth20Credentials.getCode(); logger.debug("code: {}", code); final OAuth2AccessToken accessToken; try { accessToken = this.configuration.buildService(context, client, null).getAccessToken(code); } catch (final IOException | InterruptedException | ExecutionException e) { throw new HttpCommunicationException("Error getting token:" + e.getMessage()); } logger.debug("accessToken: {}", accessToken); oAuth20Credentials.setAccessToken(accessToken); } }
@Override public <R> Future<R> execute(OAuthRequest request, OAuthAsyncRequestCallback<R> callback, OAuthRequest.ResponseConverter<R> converter) { OAuthRequest authRequest = addClientAuthentication(request); return super.execute(authRequest, callback, converter); }
final OAuth20Service service = util.getService(); final OAuthRequest request = new OAuthRequest(Verb.POST, url, service); request.addHeader("Content-Type", "application/json;charset=UTF-8"); request.addPayload(MAPPER.writeValueAsString(requestModel)); // or this if you have a json string // request.addPayload(json); service.signRequest(new OAuth2AccessToken(accessToken), request); final Response postResponse = request.send();
public void doInBackground() { try { if (service.getVersion() == "1.0") { OAuth10aService oAuth10aService = (OAuth10aService) service; requestToken = oAuth10aService.getRequestToken(); authorizeUrl = oAuth10aService.getAuthorizationUrl((OAuth1RequestToken) requestToken); } else if (service.getVersion() == "2.0") { OAuth20Service oAuth20Service = (OAuth20Service) service; authorizeUrl = oAuth20Service.getAuthorizationUrl(null); } } catch (Exception e) { this.e = e; } }
String code = authorizedUri.getQueryParameter(OAuthConstants.CODE); OAuth20Service oAuth20Service = (OAuth20Service) service; accessToken = oAuth20Service.getAccessToken(code);
final Response response = service.execute(request); result.setStatusCode(response.getCode()); switch(result.getStatusCode()) {
@Override public void signRequest(String accessToken, OAuthRequest request) { //sig = lower(md5( sorted_request_params_composed_string + md5(access_token + application_secret_key))) try { final String tokenDigest = md5(accessToken + getApiSecret()); final ParameterList queryParams = request.getQueryStringParams(); queryParams.addAll(request.getBodyParams()); final List<Parameter> allParams = queryParams.getParams(); Collections.sort(allParams); final StringBuilder stringParams = new StringBuilder(); for (Parameter param : allParams) { stringParams.append(param.getKey()) .append('=') .append(param.getValue()); } final String sigSource = URLDecoder.decode(stringParams.toString(), "UTF-8") + tokenDigest; request.addQuerystringParameter("sig", md5(sigSource).toLowerCase()); super.signRequest(accessToken, request); } catch (UnsupportedEncodingException unex) { throw new IllegalStateException(unex); } }
public String linkFor(String idpId, String redirectPage) { AbstractOAuth2AuthenticationProvider idp = authenticationSvc.getOAuth2Provider(idpId); return idp.getService(createState(idp, toOption(redirectPage) ), getCallbackUrl()).getAuthorizationUrl(); }
public OAuth2UserRecord getUserRecord(String code, String state, String redirectUrl) throws IOException, OAuth2Exception { OAuth20Service service = getService(state, redirectUrl); OAuth2AccessToken accessToken = service.getAccessToken(code); final String userEndpoint = getUserEndpoint(accessToken); final OAuthRequest request = new OAuthRequest(Verb.GET, userEndpoint, service); request.addHeader("Authorization", "Bearer " + accessToken.getAccessToken()); request.setCharset("UTF-8"); final Response response = request.send(); int responseCode = response.getCode(); final String body = response.getBody(); logger.log(Level.FINE, "In getUserRecord. Body: {0}", body); if ( responseCode == 200 ) { final ParsedUserResponse parsed = parseUserResponse(body); return new OAuth2UserRecord(getId(), parsed.userIdInProvider, parsed.username, OAuth2TokenData.from(accessToken), parsed.displayInfo, parsed.emails); } else { throw new OAuth2Exception(responseCode, body, "Error getting the user info record."); } }
final int code; try { response = service.execute(request); body = response.getBody(); code = response.getCode();
private SocialUser getAccount(OAuth20Service service, OAuth2AccessToken accessToken) { try { OAuthRequest request = new OAuthRequest(Verb.GET, getAccountUrl, service); service.signRequest(accessToken, request); Response response = request.send(); return toAccount(response.getBody()); } catch (Exception e) { return null; } }
@Override public RedirectAction redirect(final WebContext context) { try { final String state; if (configuration.isWithState()) { state = this.configuration.getStateGenerator().generateState(context); logger.debug("save sessionState: {}", state); context.getSessionStore().set(context, configuration.getStateSessionAttributeName(client.getName()), state); } else { state = null; } final OAuth20Service service = this.configuration.buildService(context, client, state); final String authorizationUrl = service.getAuthorizationUrl(this.configuration.getCustomParams()); logger.debug("authorizationUrl: {}", authorizationUrl); return RedirectAction.redirect(authorizationUrl); } catch (final OAuthException e) { throw new TechnicalException(e); } } }