Android Twitter oAuth Connect Using fabric Plugin

In the previous tutorial we have seen how to integrate twitter into android application using twitter4j library in this tutorial we will see the same instead   of using twitter4j we will use a new  plugin for android studio called as fabric. By using fabric plugin you can avoid writing tons of code , and also makes the code more easy to understand . at the end of this tutorial we will see how fabric plugin differs from twitter4j .



Create Twitter App


To enable application specific Consumer key & Consumer Secret key,
  1. Go to Twitter developer console https://apps.twitter.com/ .
  2. Click on “Create New App” .
  3. Under “Settings” tab, add the application details.
  4. Once you done with above procedure, click on “Permissions” tab to set the access “permission level”.
  5. Finally, Click on “Keys & Access Tokens” tab to get the “Consumer key” as well as “Secret Key”. 

Go to Twitter developer console https://apps.twitter.com/ Click on “Create New App”.


Fill the Application Details . if you don't have website , then fill  field fill any dummy value

 Under “Settings” tab, add the application details.


Once you done with above procedure, click on “Permissions” tab to set the access “permission level”.

Finally, Click on “Keys & Access Tokens” tab to get the “Consumer key” as well as “Secret Key”.

Install Fabric for Android Studio


Go To  "https://fabric.io/downloads" and dowload the fabric plugin for android studio .



Now in android studio go to File ->Settings ->plugins , here click on "install plugin
from disk" and browse the location of the downloaded fabric plugin to add and then restart the
android studio .




Use the Fabric Plugin


After android studio gets restart you will see the fabric icon in the toolbar click on it and then just perform the action as in the below screenshots , fabric plugin  make some changes in buidle,gradle and manifest file






Gradle


file : build.gradle
buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}


android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "androidtwitter.tutorialsbuzz.com.androidtwitterlogin"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile('com.twitter.sdk.android:twitter:1.8.0@aar') {
        transitive = true;
    }
}


XML Layout


file : activity_login.xml
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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">

    <TextView
        android:id="@+id/status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textSize="20sp" />

    <com.twitter.sdk.android.core.identity.TwitterLoginButton
        android:id="@+id/twitter_login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>


Activities


file : LoginActivity.java
package androidtwitter.tutorialsbuzz.com.androidtwitterlogin;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import com.twitter.sdk.android.Twitter;
import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterAuthConfig;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.identity.TwitterLoginButton;
import io.fabric.sdk.android.Fabric;

public class LoginActivity extends Activity {


    private static final String TWITTER_KEY = "YOUR_CONSUMER_KEY";
    private static final String TWITTER_SECRET = "YOUR_CONSUMER_SECRET_KEY";
    private TwitterLoginButton loginButton;
    private TextView status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
        Fabric.with(this, new Twitter(authConfig));

        setContentView(R.layout.activity_login);

        loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
        status = (TextView) findViewById(R.id.status);

        status.setText("Status: Ready");
        loginButton.setCallback(new LoginHandler());

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        loginButton.onActivityResult(requestCode, resultCode, data);
    }

    private class LoginHandler extends Callback<TwitterSession> {
        @Override
        public void success(Result<TwitterSession> twitterSessionResult) {
            String output = "Status: " +
                    "Your login was successful " +
                    twitterSessionResult.data.getUserName() +
                    "nAuth Token Received: " +
                    twitterSessionResult.data.getAuthToken().token;
            status.setText(output);

        }

        @Override
        public void failure(TwitterException e) {
            status.setText("Status: Login Failed");
        }
    }


}


AndroidManifest


file : AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="androidtwitter.tutorialsbuzz.com.androidtwitterlogin">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".HomeActivity"></activity>

        <meta-data
            android:name="io.fabric.ApiKey"
            android:value="64e266d2f9f77befb5dccbff44e5dfefb8e8947c" />
    </application>

    <uses-permission android:name="android.permission.INTERNET" />
</manifest>







One important difference between fabric plugin and twitter4j is while composing tweet .

  • With fabric plugin while composing tweet you leave your application because during tweet composition it checks whether default twitter app is installed or not ,if not then action competition between browser apps in device takes place .
  • With Twitter4j library while composing tweet you won't leave your application .


1 comment:

Post a Comment