Implementing the Twitter OAuth flow in Android

Updated on Thu, 2013-09-05 17:29
Overview

Add required permissions

First, let's add permission to access the internet to our app. In the "Package Explorer" pane, double-click the AndroidManifest.xml. Select the "Permissions" tab, then press the "Add..." button. Select a "uses-permission". In the "Attributes for Uses Permission", enter "android.permission.INTERNET", and then save the file.

Create our main UI

Next, let's add our "Login with Twitter" button to our main Activity. In the "Package Explorer" pane, open the "res" folder, and then "layout" folder. You should see a file called "activity_main.xml". Double-click the file to open the graphical layout. At the bottom of the layout pane, select "activity_main.xml" to see the source code for the Activity.

Replace the contents of this file with:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:gravity="center"
        android:onClick="buttonPushed"
        android:text="@string/login_with_twitter" />
</LinearLayout>

Save the file and close it.

Create our webview's UI

Right-click the "layout" folder, select New, then Android XML File. Ensure that the "Resource Type" is set to "Layout". For "File", enter "oauth_web_view.xml". Under "Root Element", select "WebView". Finally, click Finish.

[image] Replace the contents of this file with:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <WebView xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:id="@+id/webView"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent" >
  6.  
  7. </WebView>

Save the file and close it.

Create a place to store our constants Right-click the "src", "com.example.twittertutorial" package and select "Class". In the "Name" field, enter "Constants". In this file, we'll add constants that we'll reference in various aspects of our demo.
  1.     package com.example.twittertutorial;
  2.  
  3.     public class Constants {
  4.         public final static String CONSUMER_KEY = "";
  5.         public final static String CONSUMER_SECRET = "";
  6.         public final static String OAUTH_TOKEN = "oauth_token";
  7.         public final static String OAUTH_VERIFIER = "oauth_verifier";
  8.         public final static String OAUTH_DENIED = "denied";
  9.         public final static String OAUTH_CALLBACK = "twittertutorial://oauth";
  10.     }
  11.  
Create our interface Again, right-click the "src", "com.example.twittertutorial" package and select "New" / "Interface". In the "Name" field, enter "TwitterOAuthListener". Add three methods to the interface:
  1.     public void onOAuthException(Exception e);
  2.  
  3.     public void onRequestTokenReceived(String authorizationURL);
  4.  
  5.     public void onAccessTokenReceived(AccessToken accessToken);
  6.  
Finally, add an import statement to the file:
  1.     import twitter4j.auth.AccessToken;
  2.  
Create the WebView Activity Create a new class called "WebViewActivity". In the "Superclass" field, select "Browse". Enter "Activity" in the field and select the first choice, which should be "Activity - android.app". Press "OK" to create the file.