This is a mirror of official site: http://jasper-net.blogspot.com/

A 30 minute guide to integrating Twitter in your Android application.

| Tuesday, March 1, 2011
twitterLoginPage.png

Introduction
The goal of this article is to get twitter integration up & running from your Android app in 30 minutes. The guide will show you how to

  • setup a twitter test account
  • register a twitter application
  • authenticate the user in your Android application.
  • have the user send tweets from your Android application.
This guide is accompanied  by a sample application that’s available in Github in the AndroidTwitterSample repository. To import this project in Eclipse, I suggest using the EGit plugin that can be installed via the Main P2 Repository located at http://download.eclipse.org/egit/updates.


Before running this project, make sure you change the com.ecs.android.sample.twitter.Constants file to include your consumer key and consumer secret. (see subsequent section).
Once you have sample application up & running, you can copy the relevant classes into your projects to have Twitter up & running.

Twitter uses the OAuth protocol to authorize your android application to send tweets on behalf of the end-user. The end-user will need to authenticate against Twitter (meaning that your application will not capture the twitter username / password). Once the user has authorized access, you’ll be able to send tweets on behalf of the user. We’ll use signpost library to handle the OAuth communication, and the Twitter4J library to handle the Twitter specific interactions (sending tweets).


Setting up the Twitter account and application.
We’ll start by setting up a test-account on Twitter that we’ll use in our Android application. Goto the Twitter signup page and create an account. You’ll receive an  email from Twitter to confirm your account. You can skip the friends import as it will only be used for testing purposes.

Now that we have the Twitter account setup, we need to define an application. Go to the Twitter Application Registration page, and register an application. The application that your register here is required to perform Twitter interaction from your Android application. The Twitter application will have a consumer key and consumer secret associated with it that we’ll use in our Android application.

The sample application
The sample application is available in Github in the AndroidTwitterSample repository. Before running this project, make sure you change the com.ecs.android.sample.twitter.Constants file to include your consumer key and consumer secret. The application provides you with an end-to-end example on how to authenticate against Twitter and send tweets on behalf or the authenticated user.

The sample project has a dependency towards the following libraries :

signpost-commonshttp4-1.2.1.1.jar
signpost-core-1.2.1.1.jar
httpclient-4.0.1.jar
twitter4j-core-2.1.11


Dev note : You’ll need to include these libraries into your own project if you want to enable the Twitter integration.

The sample application contains 1 main activity with

a status message, indicating if you’re logged into Twitter.
a Tweet button, used to send a Tweet. When not authenticated, the application will redirect you to the Twitter loging page.
a Clear Credentials button, removing all saved credentials, forcing you to login next time you want to send a Tweet.

The Constants file
The Android project contains a Constants file containing the following information we received from Twitter when we setup our application.

public class Constants {

public static final String CONSUMER_KEY = "<FILL IN YOUR CONSUMER KEY FROM TWITTER HERE>";
public static final String CONSUMER_SECRET= "<FILL IN YOUR CONSUMER SECRET FROM TWITTER HERE>";

public static final String REQUEST_URL = "http://api.twitter.com/oauth/request_token";
public static final String ACCESS_URL = "http://api.twitter.com/oauth/access_token";
public static final String AUTHORIZE_URL = "http://api.twitter.com/oauth/authorize";

final public static String CALLBACK_SCHEME = "x-latify-oauth-twitter";
final public static String CALLBACK_URL = CALLBACK_SCHEME + "://callback";

Posted via email from Jasper-net

0 comments: