Search in sources :

Example 1 with AsyncSimpleTask

use of com.codepath.utils.AsyncSimpleTask in project android-oauth-handler by codepath.

the class OAuthAsyncHttpClient method fetchRequestToken.

// Get a request token and the authorization url
// Once fetched, fire the onReceivedRequestToken for the request token handler
// Works for both OAuth1.0a and OAuth2
public void fetchRequestToken() {
    new AsyncSimpleTask(new AsyncSimpleTask.AsyncSimpleTaskHandler() {

        String authorizeUrl = null;

        Exception e = null;

        Token requestToken;

        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;
            }
        }

        public void onPostExecute() {
            if (e != null) {
                handler.onFailure(e);
            } else {
                handler.onReceivedRequestToken(requestToken, authorizeUrl, service.getVersion());
            }
        }
    });
}
Also used : AsyncSimpleTask(com.codepath.utils.AsyncSimpleTask) OAuth1RequestToken(com.github.scribejava.core.model.OAuth1RequestToken) Token(com.github.scribejava.core.model.Token) OAuth10aService(com.github.scribejava.core.oauth.OAuth10aService) OAuthException(com.github.scribejava.core.exceptions.OAuthException) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service)

Example 2 with AsyncSimpleTask

use of com.codepath.utils.AsyncSimpleTask in project android-oauth-handler by codepath.

the class OAuthAsyncHttpClient method fetchAccessToken.

// Get the access token by exchanging the requestToken to the defined URL
// Once receiving the access token, fires the onReceivedAccessToken method on the handler
public void fetchAccessToken(final Token requestToken, final Uri uri) {
    new AsyncSimpleTask(new AsyncSimpleTask.AsyncSimpleTaskHandler() {

        Exception e = null;

        public void doInBackground() {
            // Fetch the verifier code from redirect url parameters
            Uri authorizedUri = uri;
            try {
                if (service.getVersion() == "1.0") {
                    if (authorizedUri.getQuery().contains(OAuthConstants.VERIFIER)) {
                        String oauth_verifier = authorizedUri.getQueryParameter(OAuthConstants.VERIFIER);
                        OAuth1RequestToken oAuth1RequestToken = (OAuth1RequestToken) requestToken;
                        OAuth10aService oAuth10aService = (OAuth10aService) service;
                        accessToken = oAuth10aService.getAccessToken(oAuth1RequestToken, oauth_verifier);
                    } 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;
                        accessToken = oAuth20Service.getAccessToken(code);
                    } else {
                        // verifier was null
                        throw new OAuthException("No code was returned with uri '" + uri + "' " + "and access token cannot be retrieved");
                    }
                }
            } catch (Exception e) {
                this.e = e;
            }
        }

        public void onPostExecute() {
            if (e != null) {
                handler.onFailure(e);
            } else {
                setAccessToken(accessToken);
                handler.onReceivedAccessToken(accessToken, service.getVersion());
            }
        }
    });
}
Also used : OAuth1RequestToken(com.github.scribejava.core.model.OAuth1RequestToken) OAuthException(com.github.scribejava.core.exceptions.OAuthException) AsyncSimpleTask(com.codepath.utils.AsyncSimpleTask) OAuth10aService(com.github.scribejava.core.oauth.OAuth10aService) Uri(android.net.Uri) OAuthException(com.github.scribejava.core.exceptions.OAuthException) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service)

Aggregations

AsyncSimpleTask (com.codepath.utils.AsyncSimpleTask)2 OAuthException (com.github.scribejava.core.exceptions.OAuthException)2 OAuth1RequestToken (com.github.scribejava.core.model.OAuth1RequestToken)2 OAuth10aService (com.github.scribejava.core.oauth.OAuth10aService)2 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)2 Uri (android.net.Uri)1 Token (com.github.scribejava.core.model.Token)1