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());
}
}
});
}
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());
}
}
});
}
Aggregations