Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 939
coder Avatar asked Aug 22 '11 16:08

coder


People also ask

What is Twitter OAuth?

OAuth 1.0a allows an authorized Twitter developer App to access private account information or perform a Twitter action on behalf of a Twitter account.

What is OAuth authentication in Android?

OAuth is an open standard for secure authentication, commonly used to grant websites or applications access to information on other platforms without giving them the passwords. This article shows the technical implementation of an OAuth2 Authentication on Android, using the Authorization Code Flow.

How do I get my Twitter OAuth token?

Generating access tokensLogin to your Twitter account on developer.twitter.com. Navigate to the Twitter app dashboard and open the Twitter app for which you would like to generate access tokens. Navigate to the "Keys and Tokens" page. Select 'Create' under the "Access token & access token secret" section.


1 Answers

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: x-your-application-name-oauth-twitter://callback (*).

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.

like image 189
Cristian Avatar answered Oct 02 '22 01:10

Cristian