Android Facebook GraphRequest不返回电子邮件

时间:2016-07-22 20:59:39

标签: android facebook

我已查看过有关此问题的几篇帖子,但尚未找到解决方案。

     facebookLoginButton = (LoginButton) findViewById(R.id.facebook_login_button);
        facebookLoginButton.setReadPermissions(Arrays.asList("read_stream", "user_photos", "email", "user_location"));

// create callback manager for facebook
        callbackManager = CallbackManager.Factory.create();

        facebookLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Log.d(TAG, "LOGGED IN");

                GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        Log.d(TAG, "Facebook graph response: " + response.toString());
                        try {
                            // get only the part before the @ symbol
                            String email_username = object.getString("email").substring(0, object.getString("email").indexOf("@"));
                            editor = preferences.edit();
                            editor.putString("username", email_username);
                            editor.commit();

                            Log.d(TAG, "logging in user " + preferences.getString("username", "") + " with userID: " + preferences.getString("userID", ""));
                            SpinLoginTask spinLoginTask = new SpinLoginTask(LoginActivity.this);
                            spinLoginTask.execute("facebook");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
                request.executeAsync();
            }

我收到以下回复:

Facebook graph response: {Response:  responseCode: 200, graphObject: {"name":"Spin Tester","id":"xxxxx"}, error: null}
org.json.JSONException: No value for email

由于我在按钮上设置了读取权限以提供所有信息,为什么它没有出现在Graph请求响应中?

2 个答案:

答案 0 :(得分:4)

尝试我一直使用的代码:编辑

实际上,您需要更改代码的一小部分:

    GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject object, GraphResponse response) {
               Log.d(TAG, "Facebook graph response: " + response.toString());
               try {
                      // get only the part before the @ symbol
                      String email_username = object.getString("email").substring(0, object.getString("email").indexOf("@"));
                        editor = preferences.edit();
                        editor.putString("username", email_username);
                        editor.commit();

                        Log.d(TAG, "logging in user " + preferences.getString("username", "") + " with userID: " + preferences.getString("userID", ""));
                        SpinLoginTask spinLoginTask = new SpinLoginTask(LoginActivity.this);
                        spinLoginTask.execute("facebook");
               } catch (Exception e) {
                      e.printStackTrace();
              }

           }
       });

    //ADD THIS LINES PLEASE
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,name,email");
    request.setParameters(parameters);
    request.executeAsync();
 }

答案 1 :(得分:1)

用户可以选择在首次登录时通过Facebook提供的用户界面拒绝访问其电子邮件地址。我会检查以确保电子邮件是返回的JSON结构。如果您需要电子邮件地址,请携带AlertDialog或其他UI来询问他们的电子邮件。确保你有逻辑,以防他们拒绝提供电子邮件,因为这也是一种可能性。