电子邮件没有从android sdk 4.7.0中获取

时间:2016-01-28 05:50:32

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

我想从facebook登录中获取名字,姓氏和电子邮件,然后将其传递给网络服务。但问题是我得到的是名字和姓氏,但是电子邮件。 过去两天我一直在寻找这个但是找不到它。以下是我的代码:

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

    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager=CallbackManager.Factory.create();
    setContentView(R.layout.activity_main);
    loginButton=(LoginButton)findViewById(R.id.fbLogin);
    // loginButton.setReadPermissions("user_friends");
    setContentView(R.layout.activity_main);
    viewInflation();
    if (Build.VERSION.SDK_INT > 15) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        volleySingleton = VolleySingleton.getInstance();
        requestQueue = volleySingleton.getRequestQueue();
        fbRegister.setOnClickListener(this);

        loginButton.setReadPermissions("public_profile");
        loginButton.setReadPermissions("email");
        loginButton.setReadPermissions("user_birthday");
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Profile profile = Profile.getCurrentProfile();
                System.out.println(profile.getFirstName());
                System.out.println(profile.getId());
                System.out.println(loginResult.getAccessToken().getUserId());

        System.out.println("Auth Token: " + loginResult.getAccessToken().getToken());
                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {
                                if (response.getError() != null) {
                                    // handle error
                                } else {
                                    Profile profile = Profile.getCurrentProfile();
                                    System.out.println(profile.getFirstName());
                                    System.out.println(profile.getId());
                                    String email = object.optString("email");
                                    System.out.println("Email is "+email);
                                    String fname = object.optString("fname");
                                    String lname = object.optString("lname");
                                    Log.i("Email", email);
                                    Log.i("fname", fname);
                                    Log.i("lname", lname);
                                    //     Log.d("Response", response.getInnerJsobject.toString());

                                }
                            }
                        });
                Bundle parameters = new Bundle();
                parameters.getString("fields", "id,first_name,last_name,user_birthday,email");
                request.setParameters(parameters);
                request.executeAsync();
            }

            @Override
            public void onCancel() {
                // info.setText("Login Attempt Cancelled");
            }

            @Override
            public void onError(FacebookException exception) {
                //  info.setText("Login Attempt Failed");
            }
        });
    }}

1 个答案:

答案 0 :(得分:0)

尝试使用其他Facebook帐户或拥有有效身份验证的电子邮件。 根据facebook sdk Developers Reference Guide他们提到:

  

个人资料中列出的人员主电子邮件地址。如果没有有效的电子邮件地址,则不会返回此字段

所以一定要检查一下。