3

First off let me describe what I have working...

(1) I've registered my app on the Twitter Developers site and got some OAuth credentials (Consumer Key, Consumer Secret, Request token URL etc). I tried entering Callback URL here as "callback://tweeter" (and other non-Http Urls) but page validation fails and I get a "Not a valid URL format" error message, so I left this blank.

(2) Next I set up my 'CommonsHttpOAuthConsumer' and 'OAuthProvider' objects with my OAuth credentials and empty Callback URL, and kicked off a web activity as follows:

CommonsHttpOAuthConsumer consumer = new
CommonsHttpOAuthConsumer(myTwitterConsumerKey,
myTwitterConsumerSecret);
OAuthProvider provider = new
DefaultOAuthProvider(myTwitterRequestTokenUrl,
myTwitterAccessTokenUrl, myTwitterAuthorizeUrl);
String authUrl =
provider.retrieveRequestToken(consumer, "");
startActivity(new
Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));

(3) Next I followed the web activity that's launched (from my app) and signed in to my Twitter account (and thereby acknowledged that I allow the app to access my Twitter account).

(4) Lastly, submitting my request I was redirected to a page (still within the web activity) congratulating me that I had granted my app access to my Twitter account and giving me a pin to enter where required.

Now what I would like of course is that on successful authorisation, I am redirected back to my app (the Activity that launched the web activity) and I handle the successful authorisation there.

I've tried numerous things (a couple described below) but am coming up short every time and am stuck for ideas. If someone can please help me out and let me know what I'm doing wrong, I will be indebted!

Here are the two main things I tried:

(1) I tried setting the Callback Url in my code to "callback://tweeter" (despite it being blank in my app's Twitter registration OAuth settings) but, this time, when I make the following method call...

provider.retrieveRequestToken(consumer, "callback://tweeter");

... an OAuthCommunicationException is thrown (containing message: "Communication with the service provider failed...").

(2) I tried setting the Callback Url in my app's Twitter registration OAuth setting to an Http Url (e.g. "http://myhost.com") and specifying an intent-filter in my activity manifest to capture the callback as follows...

<intent-filter>
<action android:name = "android.intent.action.VIEW" />
<category android:name ="android.intent.category.DEFAULT" />
<category android:name ="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="myhost.com" />
</intent-filter>

... but after successful authorisation, I am redirected within the web activity (internet browser) to myhost.com rather than returning back to my activity.

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
  • 1
    Adil, Made a tutorial for this: http://www.anddev.org/advanced-tutorials-f21/sending-a-tweet-t54389.html – Blundell Dec 22 '11 at 22:49
  • Success! Blundell, found in your tutorial what I was doing wrong. I was specifying the callback url as "callback://tweeter" (2 forward slashes) when it should have been was "callback:///tweeter" (i.e 3 forward slashes). In fact, for simplification, it only need be "callback:///" and the intent filter data node is then simply: ``. I am having another problem though when starting up the twitter authorisation WebView as in your tutorial (can't enter anything in the textboxes) but I'll put up a question on your tutorial page or as a separate stackoverflow question. – Adil Hussain Dec 23 '11 at 09:51
  • Yeah do it as a separate StackOverflow question] – Blundell Dec 23 '11 at 11:15
  • Solved the "can't enter anything in the [WebView] textboxes" problem and it's pretty straightforward so leaving it as a comment here. All you need to do is get the WebView to request focus when it comes into view, i.e. make the following method call: `myWebView.requestFocus(View.FOCUS_DOWN)`. – Adil Hussain Dec 27 '11 at 20:24

3 Answers3

3

Glad you found the answer at my Twitter tutorial: http://www.anddev.org/advanced-tutorials-f21/sending-a-tweet-t54389.html

For other people:

I was specifying the callback url as "callback://tweeter" (2 forward slashes) when it should have been "callback:///tweeter" (i.e 3 forward slashes).


In fact, for simplification, it only need be "callback:///" and the intent filter data node is then simply:

 <data android:scheme="callback"/>

I would not recommend that simplification as you have to remember what if another app uses it as well, your app would pick it up and all hell would break loose!

Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158
Blundell
  • 75,855
  • 30
  • 208
  • 233
  • Thanks again Blundell. Good spot. Added the `android:host` attribute with value in the following format: _myappname.com_ – Adil Hussain Dec 23 '11 at 11:51
  • Hmm, turns out adding the 'host' attribute causes my Activity not to pick up the "callback" Intent and so I'm redirected to a page in the web browser instead. I think (not sure though) it's because the redirect url (after authentication) is in the following format: "callback:///myappname.com?oauth_token=..." (i.e. with the 'oauth_token' query parameter appended) and this doesn't register wit my Activity's intent filter. Any ideas anyone how I can specify the intent-filter with 'scheme' _and_ 'host' attributes (and other attributes if need be) such that my Activity picks up the callback intent? – Adil Hussain Dec 24 '11 at 09:11
  • 1
    Have you tried a regex maybe? I'd just make the callback more unique saves the hassle :-) – Blundell Dec 24 '11 at 13:43
  • Yeah, just changed the scheme to a more unique value of the form "myappnametwittercallback". Don't want to spend more time on this than I have already... – Adil Hussain Dec 25 '11 at 12:37
  • I made a few small tweaks to Blundell's tutorial so that the Activity 'singleInstance' declaration, 'intent-filter' declaration and 'onNewIntent(Intent)' method override is not required in the manifest and such that the phone's web browser application is never launched (thereby leaving your app, which you don't want). Explained here if anyone's interested: [Twitter login using twitter4j library](http://adilatwork.blogspot.com/2011/12/android-twitter-login-using-twitter4j.html) – Adil Hussain Dec 27 '11 at 10:40
  • I've followed this and what @AdilHussain said and I'm getting " Twitter login error" java.long.nullpointerException – John Sep 18 '14 at 13:36
2

I was having this same problem. What fixed my problem was ensuring that when setting up the intent filter in the manifest, ensure that the name you use in android:scheme is all lower case letters with no underscores, although dashes are fine, like so:

<data android:scheme="your-callback-here" />

Just something I stumbled on as a newer developer!

RuideraJ
  • 71
  • 2
  • 7
0

you might b stuffed in redirect to your Application

try to solve this way If u put Callback URL wrongly thn u can't back to your application

you have to put it in twitter created App Callback URL : http://twitter.com/oauth/authorize?oauth_token=actualtokenhere&oauth_callback=myapp:///

It will work for you hopefully :)

Bluck
  • 1
  • 1