Android Facebook SDK 4电子邮件返回null

时间:2017-07-14 09:16:47

标签: android facebook-graph-api login sdk facebook-login

您好我正试图从Facebook sdk 4收到电子邮件,但到目前为止没有任何运气:(

所以我希望这里有一些能帮助我解决这个问题:)

代码只是返回电子邮件null,但我的Facebook帐户有电子邮件,应用程序可以在开发者应用审核中访问电子邮件。

这是我的代码:

public class LoginActivity extends Activity {
private CallbackManager callbackManager;
private LoginButton loginButton;
private TextView btnLogin;
private ProgressDialog progressDialog;
User user;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    if(PrefUtils.getCurrentUser(LoginActivity.this) != null){

        Intent homeIntent = new Intent(LoginActivity.this, LogoutActivity.class);

        startActivity(homeIntent);

        finish();
    }
}

@Override
protected void onResume() {
    super.onResume();


    callbackManager=CallbackManager.Factory.create();

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

    loginButton.setReadPermissions("public_profile", "email","user_friends", "user_location", "user_birthday", "user_photos");

    btnLogin= (TextView) findViewById(R.id.btnLogin);
    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            progressDialog = new ProgressDialog(LoginActivity.this);
            progressDialog.setMessage("Loading...");
            progressDialog.show();

            loginButton.performClick();

            loginButton.setPressed(true);

            loginButton.invalidate();

            loginButton.registerCallback(callbackManager, mCallBack);

            loginButton.setPressed(false);

            loginButton.invalidate();

        }
    });
}

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


private FacebookCallback<LoginResult> mCallBack = new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {

        progressDialog.dismiss();

        // App code
        final GraphRequest request = GraphRequest.newMeRequest(
                loginResult.getAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(
                            JSONObject object,
                            GraphResponse response) {

                        Log.e("response: ", response + "");
                            try {
                                user = new User();
                                user.facebookID = object.getString("id").toString();
                                //user.email = object.getString("email");
                                user.email = object.optString("email");
                                user.name = object.getString("name").toString();
                                user.gender = object.getString("gender").toString();
                                user.birthday = object.getString("birthday").toString();
                                PrefUtils.setCurrentUser(user,LoginActivity.this);

                            }catch (Exception e){
                                e.printStackTrace();
                            }
                          Toast.makeText(LoginActivity.this,"welcome "+user.name,Toast.LENGTH_LONG).show();
                            Intent intent=new Intent(LoginActivity.this,LogoutActivity.class);
                            startActivity(intent);
                            finish();

                    }

                });

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

    @Override
    public void onCancel() {
        progressDialog.dismiss();
    }

    @Override
    public void onError(FacebookException e) {
        progressDialog.dismiss();
    }
};

}

2 个答案:

答案 0 :(得分:1)

好吧,我遇到过这样的问题。在我的情况下,这是因为您使用的帐户,它是在没有电子邮件的情况下注册的,只是使用电话号码。由于facebook可以在没有电子邮件的情况下进行注册,因此出现了这个错误。

尝试下载Zalora应用程序,他们通过制作新页面重新发送电子邮件解决了这个问题

答案 1 :(得分:0)

只需添加.toString();到你评论的那一行。试试这个

                             try {
                                    user = new User();
                                    user.facebookID = object.getString("id").toString();
                                    user.email = object.getString("email").tostring;
                                    user.name = object.getString("name").toString();
                                    user.gender = object.getString("gender").toString();
                                    user.birthday = object.getString("birthday").toString();
                                    PrefUtils.setCurrentUser(user,LoginActivity.this);

                                 }

如果不起作用,请将登录权限添加为数组列表

loginManager.logInWithReadPermissions((Activity) context, Arrays.asList("public_profile","email","user_friends", "user_location", "user_birthday", "user_photos"));