Johnny
Johnny

Reputation: 3308

How to change Facebook permissions field required on Android

I would like to change email field to 'required' on my Android app when a user is trying to signup.

Do I have change settings on Facebook Account? Or is it a setting on my Android code?

I'm using com.facebook.android:facebook-android-sdk:4.14.1

Screenshot to explain my problem:

enter image description here

Upvotes: 0

Views: 512

Answers (2)

Vlad
Vlad

Reputation: 407

You cannot set the email permission as 'required'. You could alternatively post a message to the user explaining why the email is mandatory, and make a Log-In request again with the required permission. You can find the documentatation below:

https://developers.facebook.com/docs/facebook-login/android/permissions

When I need this I usually register a callback that takes care of this issue like the one below:

//manager to handle callbacks from Facebook
private CallbackManager callbackManager;

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

    callbackManager = CallbackManager.Factory.create();

    LoginManager.getInstance().registerCallback(callbackManager, getFacebookLoginCallback());
}

public FacebookCallback<LoginResult> getFacebookLoginCallback() {
    return new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(final LoginResult loginResult) {
            ICLog.e(TAG, "Facebook access token: " + loginResult.getAccessToken().getToken());
            if (loginResult.getRecentlyGrantedPermissions().contains("email")) {
                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                // Handle logged user here
                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender,birthday,first_name,last_name");
                request.setParameters(parameters);
                request.executeAsync();
            } else {
                //show popup and request e-mail permission if needed
                new AlertDialog.Builder(this)
                        .setMessage("I need email")
                        .setNegativeButton("Cancel", null)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0, int arg1) {
                                LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email"));
                            }
                        }).create().show();
            }
        }

        @Override
        public void onCancel() {
              Log.e("Facebook login", "onCancel");
        }

        @Override
        public void onError(FacebookException exception) {//log out user if any error appears
            if (exception instanceof FacebookAuthorizationException) {
                if (AccessToken.getCurrentAccessToken() != null) {
                    LoginManager.getInstance().logOut();
                }
            }
        }
    };
}

Upvotes: 1

karanatwal.github.io
karanatwal.github.io

Reputation: 3673

Use below on your Fb login button-

 loginbtn.setReadPermissions(Arrays.asList("public_profile, email,user_birthday"));

You can find the full code here.

Upvotes: 0

Related Questions