How To Integrate Your Facebook Login With Your Android Application

1 . Start a new Android studio project  and Specify your application name.(ex - LoginTest)
Select empty activity click next and click finish.
Ok now project is created.

2 . Open your browser and type developer.facebook.com and login with your facebook account. 
select create new app option -> provide application name , your contact email and select category (ex - education)
click on create App Id Button.

- Now the app id is genrated for your application.

- place generated app id in your android application(see later)

- Now go to settings option - select Add platform - > select Android ->
and provide details like your application package name , name of the class in which you are placed the login and key hashes.


- Application package name - go to manifest file and copy package path and paste
- class Name- MainActivity

- for key hashes - > download open SSL first - > and go to java bin folder and open cmd   and 
execute following command.

keytool -exportcert -alias androiddebugkey -keystore C:\Users\govind\.android\debug.keystore | C:\openssl-0.9.8k_X64\bin sha1 -binary | C:\openssl-0.9.8k_X64\bin\openssl base64

you get the key : 

now place the key facebook developer. change single sign on to yes and save the changes.
click on use this packege  name.


- Open resource folder -> values folder -> String.xml and  add new String resource 
facebook appid 

- now go to facebook documentation - go to android quick start guid - > you get some metadta 
place this metadata in your app

mainactivity.xml

<?xml version="1.0" encoding="utf-8"?><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="test.com.universal.logintest.MainActivity">

    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="50dp"        android:id="@+id/textview"        android:text="Hello World!" />
    <com.facebook.login.widget.LoginButton        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/facebutton"        android:layout_centerInParent="true"/>
</RelativeLayout>






Android menifiest.




<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="test.com.universal.logintest">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter><!-- ATTENTION: This intent was auto-generated. Follow instructions at  https://g.co/AppIndexing/AndroidStudio to publish your URLs. -->            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <!-- ATTENTION: This data URL was auto-generated. We recommend that you use the HTTP scheme.                  TODO: Change the host or pathPrefix as necessary. -->                <data                    android:host="logintest.universal.com.test"                    android:pathPrefix="/main"                    android:scheme="http" />
            </intent-filter>
        </activity>
        <meta-data android:name="com.facebook.sdk.ApplicationId"            android:value="@string/facebook_app_id"/>
    </application>
</manifest>






MainActivity.java




package test.com.universal.logintest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphRequestAsyncTask;
import com.facebook.GraphResponse;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

   LoginButton bt;
    TextView textview;
    CallbackManager callbackManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(this.getApplicationContext());
        setContentView(R.layout.activity_main);
        bt=(LoginButton) findViewById(R.id.facebutton);
textview=(TextView)findViewById(R.id.textview);
callbackManager=CallbackManager.Factory.create();
        bt.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                final AccessToken accessToken = loginResult.getAccessToken();
                GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject user, GraphResponse graphResponse) {
                        textview.setText(user.optString("email"));
                        textview.setText(user.optString("name"));
                        textview.setText(user.optString("id"));
                    }
                }).executeAsync();

                // loginResult.               // textview.setText("login"+loginResult.getAccessToken().getToken());            }

            @Override
            public void onCancel() {
            textview.setText("cancel");
            }

            @Override
            public void onError(FacebookException error) {
            }
        });

    }

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






build gradle module







apply plugin: 'com.android.application'
android {
    compileSdkVersion 24    buildToolsVersion "24.0.0"
    defaultConfig {
        applicationId "test.com.universal.logintest"        minSdkVersion 16        targetSdkVersion 24        versionCode 1        versionName "1.0"    }
    buildTypes {
        release {
            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    //testCompile 'junit:junit:4.12'    compile 'com.android.support:appcompat-v7:24.0.0'    compile 'com.facebook.android:facebook-android-sdk:4.14.0'

    }






Stirng.xml



<resources>
    <string name="app_name">LoginTest</string>
    <string name="facebook_app_id"> 439868756400305</string>

</resources>






Comments

Popular posts from this blog

Filter In Javafx

Kotlin with StandAlone Complier