Android Twitter Integration - Stuck on Login Page?
I have code from a previous Android app that I have successfully integrated with Twitter. I copied this code into a new application and changed callback-url
, consumer-key
and consumer-secret
for my new application.
Using the library twitter4j
, I can get RequestToken
mine and the authentication url like this:
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(myConsumerKey, myConsumerSecret);
RequestToken requestToken = twitter.getOAuthRequestToken(myCallbackUrl);
String authenticationUrl = requestToken.getAuthenticationURL()
RequestToken
has a nonzero token
value, a nonzero tokenSecret
value, and null secretKeySpec
. An authentication URL takes the following form:
http://api.twitter.com/oauth/authenticate?oauth_token=...
I load this url into mine WebView
and I see the following page:
This is where I am stuck. When I click the button Sign In
, only this page keeps loading. Nothing more. However, when I click the button Cancel
, I see the following page:
Only on this page, when I click a button Return to Fan League Beko BBL
, mine is called callback-url
, with a parameter denied
that has my value oauth_token
as its value. Has anyone seen something like this before and knows they can stop my token in the request from being processed.
Update 1: . I tried the authentication url and button
Sign In
from the desktop browser and it works as expected, processes the response and then calls the callback. It just fails when I try to use it inWebView
my android app. I've tried this on multiple android devices and tablets. JavaScript is included in mineWebView
too, so not it. Ideas are most welcome. I have no ideas!Update 2: I've just done some tracing code in debug mode on my
WebView
WebViewClient
, and I see that when I press the buttonSign In
,shouldOverrideUrlLoading(WebView webView, String url)
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)
. The valuesurl
these methods have are:https://api.twitter.com/oauth/authenticate
that is, the parameter hasoauth_token
been removed from the authentication identifier. Perhaps this request is beingPOST
handled as a requestGET
and hence why this one page keeps loading? Any ideas what I can do to get this buttonSign In
clicked to be processed correctly?
source to share
It's weird that my Twitter integration has started. I haven't changed my app code or my Twitter app settings. I noticed that where there was a blue button on the Twitter authentication page Sign In
, it now has a blue button Authenticate app
. So my guess is that something was changed / fixed at the end of Twitter.
source to share
I think you forgot to set the callback url from your Twitter app dashboard.
Enter the twitter api section, select your app and go to the settings tab.
If you don't do this when the user clicks login, the redirect won't happen and you won't be able to catch the verifier.
When you set the callback url, on the other hand, your webview can intercept the redirect.
NOTE. You can set any url you want, it is important to catch the oauth_verifier passed to the redirect url.
In this case, your
shouldOverrideUrlLoading
should be running.
source to share
Override onLoadResource(WebView view, String url)
from WebViewClient and use this code inside authorization action 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);
}
}
};
source to share
I recently enabled twitter sharing in my app. It works great.
I used the latest twitter4j-core-3.0.3.jar
I created a simple demo app, you can download it from the following links and go for it. http://santhoshkumaar.blogspot.in/2013/02/posting-message-on-twitter.html https://github.com/santhoshkumaar/ShareOnTwitter/
This might be helpful for you.
source to share
Have you put in a callback url?
public static final String CALLBACK_URL = "twitterapp: // connect";
check your authorization 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 a call to twitter.authorize (); Hope it helps
source to share