Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login with Facebook with Dialog in Android

How to implement this login with facebook? I've followed this tutorial https://www.androidlearning.in/facebook-login-for-android-app/ but it throws a complete activity (the traditional way).

I have facebook application installed

In applications like Memrise, Bandlab, etc. Shows me that Dialog but no in my application

Also I try with

LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList([...]);

but dont works...

I want:

Imagen

My app Show me:

Imagen

like image 477
HerberthObregon Avatar asked Apr 25 '17 05:04

HerberthObregon


People also ask

How do I enable login on Facebook?

#3: Set Up Facebook Login for Your WebsiteAt this point, you'll see Facebook Login among your website app options. Click the Set Up button to get started. Next, you'll fill in the information about how and where you'll use the app. You can add the Facebook Login feature on any app across multiple devices.

How do I use OAuth on Facebook?

In the App Dashboard, choose your app and scroll to Add a Product Click Set Up in the Facebook Login card. Select Settings in the left side navigation panel and under Client OAuth Settings, enter your redirect URL in the Valid OAuth Redirect URIs field for successful authorization.

What is Facebook callback URL?

The "Callback URL" is the URL that will be contacted once the user has accepted or rejected the OAuth request. This is set as a parameter of your OAuth request. So you set the URL in your own program, not somewhere in Facebook.


1 Answers

I was facing the same problem as you 30 minutes ago, but I was using a custom button for Facebook and the following code inside onCreate for calling Facebook Login:

loginButton = (CircularProgressButton) findViewById(R.id.btn_fb);

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "user_friends"));
            }
        });

        //Register a callback
        callbackManager = CallbackManager.Factory.create();

        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(final LoginResult loginResult) {
                        GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(JSONObject object,GraphResponse response) {
                                        try {
                                            nome = object.getString("name");
                                            email = object.getString("email");
                                            String idfb  = loginResult.getAccessToken().getUserId();
                                            logarFb(idfb, nome, email);

                                        } catch(JSONException ex) {
                                            ex.printStackTrace();
                                        }
                                    }
                                });
                        Bundle parameters = new Bundle();
                        parameters.putString("fields", "id,name,email,gender, birthday");
                        request.setParameters(parameters);
                        request.executeAsync();
                    }

                    @Override
                    public void onCancel() {
                        //cancelled
                    }

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

I changed it to Facebook Login button:

<com.facebook.login.widget.LoginButton
    android:id="@+id/btn_fb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="30dp"
    android:layout_marginBottom="30dp" /> 

Now that's how my code looks like now, much cleaner:

private LoginButton loginButton;
private CallbackManager callbackManager;

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    callbackManager = CallbackManager.Factory.create();

        loginButton = findViewById(R.id.btn_fb);
        loginButton.setReadPermissions("email");
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                getUserDetails(loginResult);
            }

            @Override
            public void onCancel() {
                funcoes.aviso(MainActivity.this,"Você cancelou o login",R.color.red500,3000, R.drawable.ic_triste);
            }

            @Override
            public void onError(FacebookException exception) {
                funcoes.dialogoMsg(MainActivity.this,"Há algo de errado com o login do Facebook :/");
            }
        });
}

 protected void getUserDetails(final LoginResult loginResult) {
        GraphRequest data_request = GraphRequest.newMeRequest(
                loginResult.getAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted( JSONObject object, GraphResponse response) {
                        try {
                            nome = object.getString("name");
                            email = object.getString("email");
                            String idfb  = loginResult.getAccessToken().getUserId();
                            logarFb(idfb, nome, email);

                        } catch(JSONException ex) {
                            ex.printStackTrace();
                        }
                    }

                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,email,gender, birthday");
        data_request.setParameters(parameters);
        data_request.executeAsync();

    }

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

If do you have Facebook App installed, that's should be enough to have the Facebook Login as Dialog Box instead of an FullScreen Activity.

If you want so, follow this guide If you forgot some other setting: https://www.studytutorial.in/android-facebook-integration-and-login-tutorial

like image 133
Alexandre Prazeres Avatar answered Oct 24 '22 11:10

Alexandre Prazeres