Android - Facebook API不返回任何电子邮件

时间:2015-09-11 21:21:50

标签: android facebook

继承我使用的方法,它返回id和名称没有问题。

    // Private method to handle Facebook login and callback
    private void onFblogin() {
        callbackmanager = CallbackManager.Factory.create();

        // Set permissions
        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "public_profile"));

        LoginManager.getInstance().registerCallback(callbackmanager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {

                        System.out.println("Success");
                        GraphRequest.newMeRequest(
                                loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(JSONObject json, GraphResponse response) {
                                        if (response.getError() != null) {
                                            // handle error
                                            System.out.println("ERROR");
                                        } else {
                                            System.out.println("Success");
                                            try {

                                                String jsonresult = String.valueOf(json);
                                                System.out.println("JSON Result" + jsonresult);

                                                // String str_email = json.getString("email");
                                                String str_email = json.getString("email");
                                                String str_name = json.getString("name");
                                                String str_id = json.getString("id");
//

                                                // Save your info
                                                settings = getSharedPreferences("login_details", 0);
                                                SharedPreferences.Editor editor = settings.edit();
                                                editor.putString("name", str_name);
                                                editor.putString("email", str_email);
                                                editor.commit();



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

                                }).executeAsync();

                        Intent openNewActivity = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(openNewActivity);

                    }

                    @Override
                    public void onCancel() {
                        Log.d("TAG_CANCEL", "On cancel");
                    }

                    @Override
                    public void onError(FacebookException error) {
                        Log.d("TAG_ERROR", error.toString());
                    }
                });





    }

我遇到的问题是电子邮件不会返回。

我已经使用另一个可以在我登录时收到我的电子邮件的应用程序测试了权限,因此权限似乎很好。我假设我现在称它为错误。

1 个答案:

答案 0 :(得分:1)

在执行AsyncTask之前尝试这个。

您可以在FacebookCallback()的 onSuccess()方法中执行此操作。

 GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback(){
    @Override
    public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
             try {
                 email = jsonObject.getString("email");

                 fullName = jsonObject.getString("name");          


              } catch (Exception e) {
                    Log.d("FacebookActivity", "onCompleted - undetermined FB exception");
              }

           }
     });

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

@Override public void onCancel(){}

@Override public void onError(){}
祝你好运!