如何更改Android

时间:2017-01-09 10:44:39

标签: java android facebook facebook-graph-api

当用户尝试注册时,我想在我的Android应用上将电子邮件字段更改为“必需”。

我在Facebook帐户上有更改设置吗?或者它是我的Android代码的设置?

我正在使用com.facebook.android:facebook-android-sdk:4.14.1

屏幕截图来解释我的问题:

enter image description here

2 个答案:

答案 0 :(得分:1)

您无法将电子邮件权限设置为“必需”。您也可以向用户发送消息,说明电子邮件是强制性的,并使用所需权限再次发出登录请求。您可以在下面找到文档:

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

当我需要这个时,我通常会注册一个回调来解决这个问题,如下所示:

//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();
                }
            }
        }
    };
}

答案 1 :(得分:0)

在Fb登录按钮上使用以下内容 -

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

您可以找到完整的代码here