Here i try to solve facebook integration with firebase authentication.
First you need to create project in facebook developer and get facebook key and also create project in firebase console and get google-services.json file and put into app in project structure.
Add dependency for facebook sdk and firebase authentication in build.gradle(app level).
compile 'com.facebook.android:facebook-android-sdk:4.+'
compile 'com.google.firebase:firebase-auth:11.6.2'
compile 'com.google.android.gms:play-services-auth:11.6.2'
apply plugin: 'com.google.gms.google-services'
Now add google play services in build.gradle(proj level).
classpath 'com.google.gms:google-services:3.1.0'
Add facebook key in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"/>
<activity android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name" />
activity_login.xml
<Button
android:id="@+id/btnSignInFaceBook"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
LoginActivity.class
public class LoginActivity extends AppCompactActivity{
Button faceBookBtn;
String fbId = "",fbemail="";
private FirebaseAuth mAuth;
//Facebook Declaration
CallbackManager callbackManager;
LoginManager loginManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
initUI();
}
private void initUI(){
faceBookBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
faceBookLogin();
}
});
}
private void faceBookLogin() {
loginManager = LoginManager.getInstance();
loginManager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(final LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
if (AccessToken.getCurrentAccessToken() != null) {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
if (object != null) {
try {
AppEventsLogger logger = AppEventsLogger.newLogger(LoginActivity.this);
logger.logEvent("Facebook login suceess");
handleFacebookAccessToken(loginResult.getAccessToken());
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday, about");
request.setParameters(parameters);
request.executeAsync();
}
}
@Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
}
@Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
}
});
loginManager.logInWithReadPermissions(LoginActivity.this, Arrays.asList("email", "public_profile"));
}
private void handleFacebookAccessToken(AccessToken token) {
Log.d(TAG, "handleFacebookAccessToken:" + token);
mAuth = FirebaseAuth.getInstance();
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
Log.i(TAG, "email" + user.getEmail());
}
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
}
}
});
}
}
And for logout.
FirebaseAuth.getInstance().signOut();
Hope this will help you :-)