I've been using following a tutorial to make a basic Twitter client, and all I'm trying to do is get the OAuth to work. It takes me to the consent page, but after I click "accept", it doesn't redirect me back to my app. Here's my code:
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
requestToken = twitter
.getOAuthRequestToken(TWITTER_CALLBACK_URL);
Test.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse(requestToken.getAuthenticationURL())));
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
I tried changing the value of TWITTER_CALLBACK_URL, but that didn't help. I followed a suggestion to make a WebView instead of launching the browser, but that didn't work either, since I got an error saying that "All WebView methods must be called on the same thread."
I tried calling the method in the UI thread, like this:
final WebView wv = (WebView) findViewById(R.id.wv);
mAppActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
requestToken = twitter
.getOAuthRequestToken(TWITTER_CALLBACK_URL);
wv.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
wv.loadUrl(requestToken.getAuthorizationURL());
return true;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
But this caused a NetworkOnMainThreadException. I also tried implementing an Async task, as shown below, but absolutely nothing happened when I clicked the button which called the method of which this is a snippet, not even any exceptions:
private class LoadURLAsync extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
WebView wv = (WebView) findViewById(R.id.wv);
wv.loadUrl(urls[0]);
return null;
}
}
try {
// get the access token
mAppActivity.this.accessToken =
twitter.getOAuthAccessToken(
requestToken, verifier);
LoadURLAsync task = new LoadURLAsync();
task.execute(new String[] {requestToken.getAuthorizationURL()});
} catch (Exception e) {
e.printStackTrace();
}
I've looked at the following answers, but they I still haven't been able to figure out how to get back to the app from the browser, or a way to get the WebView to work. If anyone has a solution to either of these problems, that would be great, thanks!
WebView Twitter login window on UI thread How to fix android.os.NetworkOnMainThreadException? OAuth + Twitter on Android: Callback fails