Integrasi login Facebook di Android

Tutorial Android

Integrasi login Facebook di Android

Dalam contoh ini kita akan membahas Cara memverifikasi nomor ponsel di android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity"

android:gravity="center"

android:orientation="vertical">

<TextView

android:id="@+id/txtUsername"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=""

android:padding="8dp"

android:textSize="25sp"

android:textColor="#000"/>

<com.facebook.login.widget.LoginButton

android:id="@+id/login_button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:layout_marginTop="30dp"

android:layout_marginBottom="30dp"

/>

</LinearLayout>

 

activity_user.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".UserActivity">

</androidx.constraintlayout.widget.ConstraintLayout>

 

MainActivity.java

package com.example.facebookloginexample;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;

import android.content.pm.PackageInfo;

import android.content.pm.PackageManager;

import android.content.pm.Signature;

import android.os.Bundle;

import android.util.Base64;

import android.util.Log;

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

import com.facebook.Profile;

import com.facebook.ProfileTracker;

import com.facebook.appevents.AppEventsLogger;

import com.facebook.login.LoginManager;

import com.facebook.login.LoginResult;

import com.facebook.login.widget.LoginButton;

import org.json.JSONException;

import org.json.JSONObject;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

private LoginButton loginButton;

private CallbackManager callbackManager;

private TextView txtUsername;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

txtUsername = findViewById(R.id.txtUsername);

//check login status

AccessToken accessToken = AccessToken.getCurrentAccessToken();

boolean isLoggedIn = accessToken != null && !accessToken.isExpired();

// LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile"));

if (isLoggedIn) {

//Using Graph API

LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile"));

}else{

txtUsername.setText("");

}

callbackManager = CallbackManager.Factory.create();

loginButton = (LoginButton) findViewById(R.id.login_button);

callbackManager = CallbackManager.Factory.create();

ProfileTracker profileTracker = new ProfileTracker() {

@Override

protected void onCurrentProfileChanged(

Profile oldProfile,

Profile currentProfile) {

if(currentProfile == null){

txtUsername.setText("");

}

}

};

LoginManager.getInstance().registerCallback(callbackManager,

new FacebookCallback() {

@Override

public void onSuccess(LoginResult loginResult) {

boolean loggedIn = AccessToken.getCurrentAccessToken() == null;

// App code

Log.d("loggedIn", String.valueOf(loggedIn));

Bundle parameters = new Bundle();

parameters.putString("fields", "id, first_name, last_name ,email, gender, birthday");

GraphRequest gr = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {

@Override

public void onCompleted(JSONObject object, GraphResponse response) {

if (response.getError() != null) {

// handle error

System.out.println("ERROR " + response.getError().toString());

} else {

try {

String first_name = object.getString("first_name");

String last_name = object.getString("last_name");

txtUsername.setText("Welcome, " + first_name + " " + last_name);

//txtEmail.setText(email);

} catch (JSONException e) {

e.printStackTrace();

}

}

}

});

gr.setParameters(parameters);

gr.executeAsync();

}

@Override

public void onCancel() {

// App code

}

@Override

public void onError(FacebookException exception) {

// App code

}

});

try {

PackageInfo info = getPackageManager().getPackageInfo(

"com.example.facebookloginexample",

PackageManager.GET_SIGNATURES);

for (Signature signature : info.signatures) {

MessageDigest md = MessageDigest.getInstance("SHA");

md.update(signature.toByteArray());

Log.d("KeyHash", Base64.encodeToString(md.digest(), Base64.DEFAULT));

}

} catch (PackageManager.NameNotFoundException e) {

} catch (NoSuchAlgorithmException e) {

}

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

callbackManager.onActivityResult(requestCode, resultCode, data);

super.onActivityResult(requestCode, resultCode, data);

}

private void getUserProfile(AccessToken currentAccessToken, LoginResult loginResults){

// private void getUserProfile(AccessToken currentAccessToken, LoginResult loginResults){

GraphRequest request = GraphRequest.newMeRequest(

loginResults.getAccessToken(),

new GraphRequest.GraphJSONObjectCallback() {

@Override

public void onCompleted(

JSONObject object,

GraphResponse response) {

// Application code

Log.v("LoginActivity", response.toString());

try {

String first_name = object.getString("first_name");

String last_name = object.getString("last_name");

String email = object.getString("email");

String id = object.getString("id");

txtUsername.setText("First Name: " + first_name + "\nLast Name: " + last_name);

} catch (JSONException e) {

e.printStackTrace();

}

//

}

});

Bundle parameters = new Bundle();

parameters.putString("fields", "id,name,email,gender, birthday");

request.setParameters(parameters);

request.executeAsync();

request.executeAsync();

}

}

 

UserActivity.java

package com.example.facebookloginexample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class UserActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_user);

}

}

 

 

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *