@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 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); } }
String code = authorizedUri.getQueryParameter(OAuthConstants.CODE); OAuth20Service oAuth20Service = (OAuth20Service) service; accessToken = oAuth20Service.getAccessToken(code);
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."); } }
@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 public OAuth2UserRecord getUserRecord(String code, String state, String redirectUrl) throws IOException, OAuth2Exception { OAuth20Service service = getService(state, redirectUrl); OAuth2AccessToken accessToken = service.getAccessToken(code);
MyLog.d(this, "Auth code is: " + authCode); final OAuth20Service service = state.getAccount().getOAuthService().getService(true); final OAuth2AccessToken token = service.getAccessToken(authCode); accessToken = token.getAccessToken(); accessSecret = token.getRawResponse();