com.github.scribejava.core.exceptions.OAuthException Java Examples

The following examples show how to use com.github.scribejava.core.exceptions.OAuthException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GitHubApiHelper.java    From gdx-texture-packer-gui with Apache License 2.0 5 votes vote down vote up
/** Beware: there is no timeout for browser GitHub authorization and in case user closed/left
 * authorization page without completing whole process, there will be no feedback in {@link CreateIssueResultHandler}. */
public void createIssue(final String title, final String body, final CreateIssueResultHandler resultHandler) {
    if (!checkApiKey()) {
        resultHandler.onError(new IllegalStateException("GitHub API key is invalid."));
        return;
    }

    authCallbackHandler.setListener(new AuthCallbackHandler.Listener() {
        @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);
            }
        }
    });
    Sys.openURL(apiService.getAuthorizationUrl());
}
 
Example #2
Source File: OAuthTokenClient.java    From android-oauth-handler with MIT License 4 votes vote down vote up
public void fetchAccessToken(final Token requestToken, final Uri uri) {

        Uri authorizedUri = uri;

        if (service.getVersion() == "1.0") {
            // Use verifier token to fetch access token

            if (authorizedUri.getQuery().contains(OAuthConstants.VERIFIER)) {
                String oauth_verifier = authorizedUri.getQueryParameter(OAuthConstants.VERIFIER);
                OAuth1RequestToken oAuth1RequestToken = (OAuth1RequestToken) requestToken;
                OAuth10aService oAuth10aService = (OAuth10aService) service;

                oAuth10aService.getAccessTokenAsync(oAuth1RequestToken, oauth_verifier,
                        new OAuthAsyncRequestCallback<OAuth1AccessToken>() {

                            @Override
                            public void onCompleted(OAuth1AccessToken oAuth1AccessToken) {
                                setAccessToken(oAuth1AccessToken);
                                handler.onReceivedAccessToken(oAuth1AccessToken, service.getVersion());
                            }

                            @Override
                            public void onThrowable(Throwable e) {
                                handler.onFailure(new OAuthException(e.getMessage()));
                            }
                        });

            }
            else { // verifier was null
                throw new OAuthException("No verifier code was returned with uri '" + uri + "' " +
                        "and access token cannot be retrieved");
            }
        } else if (service.getVersion() == "2.0") {
            if (authorizedUri.getQuery().contains(OAuthConstants.CODE)) {
                String code = authorizedUri.getQueryParameter(OAuthConstants.CODE);
                OAuth20Service oAuth20Service = (OAuth20Service) service;
                oAuth20Service.getAccessToken(code, new OAuthAsyncRequestCallback<OAuth2AccessToken>() {
                    @Override
                    public void onCompleted(OAuth2AccessToken accessToken) {
                        setAccessToken(accessToken);
                        handler.onReceivedAccessToken(accessToken, service.getVersion());

                    }

                    @Override
                    public void onThrowable(Throwable t) {

                    }
                });
            }
            else { // verifier was null
                handler.onFailure(new OAuthException("No code was returned with uri '" + uri + "' " +
                        "and access token cannot be retrieved"));
            }
        }
    }