Facebook Android SDK即使在获得许可后也无电子邮件

时间:2017-05-29 10:25:07

标签: android facebook facebook-sdk-4.0

我试图通过Facebook应用程序向用户发送电子邮件。但它总是把它归还空。我甚至已经获得了所需的权限。应用程序还会显示应用程序将向用户提供电子邮件等消息。

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);
}

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

1 个答案:

答案 0 :(得分:3)

而不是:

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是公开的。