How does Twitter OAuth work in Android?
I have been searching for an answer for quite some time, and I have admit that I am pretty stuck. I am attempting to implement Twitter in my application and have found it somewhat challenging. It seems the way to do it is to register my app with Twitter(I have already done so) as a Browser application. I don’t know what to use for my callback url though. I really want to just return to my current activity in the application, and not be redirected to a callback url. What should I put for that? I am pretty stuck…
One Solution collect form web for “How does Twitter OAuth work in Android?”
This is how it works… you do the call to twitter authentication URL (by opening a web browser). The URL of the authentication must contain the callback URL. A callback URL usually looks like this:
Second step is to add an intent filter to your
Activity (implementing twitter auth requires you to know how Android works (unless you find a tutorial that does everything for you, but I think that’s not the case seems you seem to be a smart guy, aren’t you?)). Whatever, you do so by adding something like this in your manifest:
<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="x-your-application-name-oauth-twitter" android:host="callback"/> </intent-filter>
This is basically a way to say to Android OS: “hey dude, I can handle any URL that looks like
x-your-application-name-oauth-twitter://callback“. That way, once user has authenticated, twitter will call that URL and your application will reclaim the control.
Usually, your activity must be created with the
android:launchMode="singleTask" tag, then you must override the
onNewIntent method of your activity that will be called once your application has the control again. Inside the
Intent you will find information about the callback.
(*) Building the URL that you must launch (and that will allow users to authenticate) is somehow difficult. OAuth is a good but kind of hard to learn standard. So, you can use third-party libraries that will help you on this. You could, for instance, use Oauth Signpost java library. However, I would recommend you to stick to twitter4j library that will help you with OAuth and also allows you to interact with Twitter API.