Facebook Android SDK 即使经过许可也没有电子邮件

Facebook Android SDK No Email even after permission

我正在尝试通过 Facebook 应用获取用户电子邮件。但它总是 return 它是空的。我什至已经授予了所需的权限。应用程序还显示应用程序将收到电子邮件等消息给用户。

public class MainActivity extends AppCompatActivity {

private TextView info;
private LoginButton loginButton;
private CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (BuildConfig.DEBUG) {
        FacebookSdk.setIsDebugEnabled(true);
        FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
    }
    callbackManager = CallbackManager.Factory.create();
    setContentView(R.layout.activity_main);
    info = (TextView)findViewById(R.id.info);
    loginButton = (LoginButton)findViewById(R.id.login_button);
    loginButton.setReadPermissions(Arrays.asList("email","user_friends"));
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
        final AccessToken accessToken= loginResult.getAccessToken();
            GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject user, GraphResponse graphResponse) {
                    info.setText(
                            "User ID: "
                                    + user.optString("email")
                                    + "\n"
                                    + user.optString("name")
                                    + "\n" +
                                    "Auth Token: "
                                    + accessToken.getToken()
                    );


                }
            }).executeAsync();

        }

        @Override
        public void onCancel() {
            info.setText("Login attempt canceled.");
        }

        @Override
        public void onError(FacebookException e) {

            info.setText("Login attempt failed.");
        }
    });

}

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

} 知道我在这里做错了什么吗?

而不是:

executeAsync();

使用:

请求的捆绑参数:

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

在您的代码中:

final AccessToken accessToken= loginResult.getAccessToken();
        GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject user, GraphResponse graphResponse) {
                info.setText(
                        "User ID: "
                                + user.optString("email")
                                + "\n"
                                + user.optString("name")
                                + "\n" +
                                "Auth Token: "
                                + accessToken.getToken()
                );


            }
         });

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

还要确保您的 Facebook 帐户中的 Email 是 public。