use of se.akerfeldt.okhttp.signpost.OkHttpOAuthProvider in project Parse-SDK-Android by ParsePlatform.
the class Twitter method authorize.
public void authorize(final Context context, final AsyncCallback callback) {
if (getConsumerKey() == null || getConsumerKey().length() == 0 || getConsumerSecret() == null || getConsumerSecret().length() == 0) {
throw new IllegalStateException("Twitter must be initialized with a consumer key and secret before authorization.");
}
final OkHttpOAuthProvider provider = new OkHttpOAuthProvider(REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTHORIZE_URL);
final OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer(getConsumerKey(), getConsumerSecret());
final ExecutorService executor = Executors.newSingleThreadExecutor();
final Handler handler = new Handler(Looper.getMainLooper());
executor.execute(() -> {
Throwable error = null;
String requestToken = null;
try {
requestToken = provider.retrieveRequestToken(consumer, callbackUrl);
} catch (Throwable e) {
error = e;
}
final Throwable finalError = error;
final String finalRequestToken = requestToken;
handler.post(() -> {
if (finalError != null) {
callback.onFailure(finalError);
return;
}
OAuth1FlowDialog dialog = new OAuth1FlowDialog(context, finalRequestToken, callbackUrl, "api.twitter", new FlowResultHandler() {
@Override
public void onError(int errorCode, String description, String failingUrl) {
callback.onFailure(new OAuth1FlowException(errorCode, description, failingUrl));
}
@Override
public void onComplete(String callbackUrl) {
CookieManager.getInstance().flush();
final Uri uri = Uri.parse(callbackUrl);
final String verifier = uri.getQueryParameter(VERIFIER_PARAM);
if (verifier == null) {
callback.onCancel();
return;
}
executor.execute(() -> {
Throwable error = null;
HttpParameters parameters = null;
try {
provider.retrieveAccessToken(consumer, verifier);
parameters = provider.getResponseParameters();
} catch (Throwable e) {
error = e;
}
final Throwable finalError = error;
final HttpParameters finalParameters = parameters;
handler.post(() -> {
if (finalError != null) {
callback.onFailure(finalError);
return;
}
try {
setAuthToken(consumer.getToken());
setAuthTokenSecret(consumer.getTokenSecret());
setScreenName(finalParameters.getFirst(SCREEN_NAME_PARAM));
setUserId(finalParameters.getFirst(USER_ID_PARAM));
} catch (Throwable e) {
callback.onFailure(e);
return;
}
callback.onSuccess(Twitter.this);
});
});
}
@Override
public void onCancel() {
callback.onCancel();
}
});
dialog.show();
});
});
}
Aggregations