Pregunta

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.

¿Fue útil?

Solución

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!

Otros consejos

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!

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 :)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top