Strangely my Twitter integration has started working. I didn't change my application code or my application's Twitter settings. I've noticed that where the Twitter authenticate page had a blue Sign In
button previously, it now has a blue Authenticate app
button. So I'm guessing something was changed/fixed on Twitter's end.
Android Twitter integration - stuck in the sign in page?
-
09-03-2022 - |
Question
I have code from a previous Android app which I successfully integrated with Twitter. I've copied this code over to a new app and changed the callback-url
, consumer-key
and consumer-secret
for my new app.
Using the twitter4j
library I'm able to get my RequestToken
and authentication url as follows:
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(myConsumerKey, myConsumerSecret);
RequestToken requestToken = twitter.getOAuthRequestToken(myCallbackUrl);
String authenticationUrl = requestToken.getAuthenticationURL()
The RequestToken
has a non-null token
value, a non-null tokenSecret
value, and a null secretKeySpec
value. The authentication url is of the following form:
http://api.twitter.com/oauth/authenticate?oauth_token=...
I load this url in my WebView
and I see the following page:
Here I'm stuck. When I click the Sign In
button, just this same page keeps loading up. Nothing else. When I click the Cancel
button however, I see the following page:
Only on this page when I click the Return to Fan League Beko BBL
button is my callback-url
invoked, with a denied
parameter which has my oauth_token
as its value. Anyone seen anything like this before and know what might be stopping my sign in request from being processed???
Update 1: I've tried the authentication url and the
Sign In
button from a desktop browser and it works as expected, processing the response and then invoking the callback-url. It's just failing when trying it in theWebView
of my Android app. I've tried it on multiple Android devices and tablets. JavaScript is enabled on myWebView
too so that's not it. Ideas most welcome. I'm out of ideas!Update 2: I've just done some debug-mode code-tracing on my
WebView
'sWebViewClient
and I'm seeing that when I click theSign In
button, theshouldOverrideUrlLoading(WebView webView, String url)
method is not called, but the following three methods are called (in order):onPageStarted(WebView view, String url, Bitmap favicon)
,onLoadResource(WebView view, String url)
,onPageFinished(WebView view, String url)
. Theurl
value these methods have is:https://api.twitter.com/oauth/authenticate
, i.e. theoauth_token
parameter has been stripped off the authenticate url. Maybe this is aPOST
request being treated as aGET
request and hence why this one same page keeps loading up? Any ideas what I can do to have thisSign In
button press processed properly?
Solution 4
OTHER TIPS
I guess you forgot to set a callback url from twitter app control panel.
Log into twitter api section, choose your app and go to settings tab.
If you don't, when the user press login no redirect will happen and thus you will not be able to catch the verifier.
When you set a callback url on the other hand, your webview can intercept the redirect.
NOTE: You can set whatever url you want, the important thing is to catch the oauth_verifier parameter passed to the redirect url.
In that case your
shouldOverrideUrlLoading
should be triggered.
Override onLoadResource(WebView view, String url)
from WebViewClient and use this code inside Authorization Activity where you use webView.loadUrl(authenticationUrl)
in onResume() and webView.setWebViewClient(webViewClient)
in onCreate().
private WebViewClient webViewClient = new WebViewClient() {
@Override
public void onLoadResource(WebView view, String url) {
// the URL we're looking for looks like this:
// callbackurl?oauth_token=1234567890qwertyuiop
Uri uri = Uri.parse(url);
if (uri.getHost().equals("callbackurlhost")) {
String token = uri.getQueryParameter("oauth_token");
if (null != token) {
webView.setVisibility(View.INVISIBLE);
AccessToken accessToken = twitter.getOAuthAccessToken();
// TODO store access token
finish();
} else {
// TODO tell user to try again
}
} else {
super.onLoadResource(view, url);
}
}
};
I have integrated sharing on twitter in my application recently. Its working perfectly.
I have used latest twitter4j-core-3.0.3.jar
I have created a simple demo application, you can download it from following links and go thorugh it. http://santhoshkumaar.blogspot.in/2013/02/posting-message-on-twitter.html https://github.com/santhoshkumaar/ShareOnTwitter/
This might be helpful to you.
Have you put the callback url?
public static final String CALLBACK_URL = "twitterapp://connect";
check your authorize method:
mHttpOauthConsumer = new CommonsHttpOAuthConsumer(mConsumerKey, mSecretKey);
mHttpOauthprovider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token",
"http://twitter.com/oauth/access_token",
"http://twitter.com/oauth/authorize");
public void authorize()
{
mProgressDlg.setMessage("Initializing ...");
mProgressDlg.show();
new Thread() {
@Override
public void run() {
String authUrl = "";
int what = 1;
try {
authUrl = mHttpOauthprovider.retrieveRequestToken(mHttpOauthConsumer, CALLBACK_URL);
what = 0;
Log.d(TAG, "Request token url " + authUrl);
} catch (Exception e) {
Log.d(TAG, "Failed to get request token");
e.printStackTrace();
}
mHandler.sendMessage(mHandler.obtainMessage(what, 1, 0, authUrl));
}
}.start();
}
make call to twitter.authorize(); Hope this helps