[SOLVED, but I'm open to new suggestions...]
I'm integrating Twitter into my Android app using twitter4j.
When I try to authorize with Twitter, I am calling the following endpoint with my oauth token:
https://api.twitter.com/oauth/authenticate?oauth_token=MY_VALID_TOKEN
which should redirect me to:
MY-CALLBACK:///?oauth_token=***&oauth_verifier=***
but instead, it redirects me to:
https://api.twitter.comMY-CALLBACK///?oauth_token=***&oauth_verifier=***
which is obviously not a valid url.
(Also, the :
is missing - it should be MY-CALLBACK:///...
)
Please note I'm using WebView for my calls
I could manipulate this string to make everything work, but there has to be a better way...
I am passing my callback URL to
getOAuthRequestToken("MY-CALLBACK:///");
and have already set the intent-filter for my activity with
<data android:scheme="x-oauthflow-twitter" />
Also, the activity has android:launchMode="singleInstance"
What am I doing wrong?
[edit:more details]
mTwitter = new TwitterFactory().getInstance();
mTwitter.setOAuthConsumer(Constants.TWITTER_CONSUMER_KEY, Constants.TWITTER_CONSUMER_SECRET);
twitterWebView = new WebView(ActivityTwitterAuthorize.this);
twitterWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith(Constants.TWITTER_CALLBACK_URL)) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
// HACKY PART!
// I added the following code to force it to work, but this is a dirty hack...
// String TWITTER_CALLBACK_INVALID_PREFIX = "https://api.twitter.comx-oauthflow-twitter///";
// TWITTER_CALLBACK_URL = "MY-CALLBACK:///";
// BEGIN
} else if (url.startsWith(TWITTER_CALLBACK_INVALID_PREFIX)) {
url = url.substring(TWITTER_CALLBACK_INVALID_PREFIX.length());
url = Constants.TWITTER_CALLBACK_URL + url;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
// END
} else {
view.loadUrl(url);
}
return true;
}
});
mTwitterReqToken = mTwitter.getOAuthRequestToken(Constants.TWITTER_CALLBACK_URL);
twitterWebView.loadUrl(mTwitterReqToken.getAuthenticationURL());
WITHOUT the hacky part, this code results in "Webpage not available" error, because the url is invalid:
https://api.twitter.comMY-CALLBACK///?oauth_token=***&oauth_verifier=***
If the url was MY-CALLBACK:///?oauth_token=***&oauth_verifier=***
then my activity would receive an Intent, and everything would be ok...
WITH the "hacky part", my code works, but I would like to avoid that piece of code.
https://api.twitter.com/oauth/authenticate?oauth_token=MY_VALID_TOKEN
in my browser, the Twitter api returns invalid url... – Bojan Radivojevic Bomber