Facebook API不会返回电子邮件

时间:2017-05-16 13:52:14

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

我知道这个问题已被提出,但只有一个答案很多,甚至没有答案。

我不知道这是不是我缺乏理解或缺乏Facebook文档,但到目前为止我有这个代码来检索用户电子邮件。

callbackManager = CallbackManager.Factory.create();
    mFbLoginManager.registerCallback(
            callbackManager,
            new FacebookCallback < LoginResult > () {
                @Override
                public void onSuccess(final LoginResult loginResult) {
                    // Handle success
                    Log.i(TAG, "callBack Login Result: LoginManager success - " + loginResult.toString());
                    GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject userObject, GraphResponse response) {
                            if (response.getError() == null) {
                                try {
                                    AccessToken token = AccessToken.getCurrentAccessToken();
                                    Log.e(TAG, token.getToken());
                                    Log.e(TAG, userObject.toString());
                                    email = userObject.getString("email");
                                } catch (JSONException ex) {
                                    Log.e(TAG, "Not such permissions for email!");
                                    ex.printStackTrace();
                                }
                                Log.d(TAG, "Email fetched: " + email);
                            }else{
                                Log.e(TAG, "Something went wrong with fetching email with GraphRequest");
                            }
                        }
                    }).executeAsync();
                }

JSON字符串仅返回名称和ID,因此我的电子邮件变量为空。

我感到困扰的部分是,当我在Graph Explorer或link上测试时,我会收到电子邮件。

我在开发者网站仪表板上以及我的代码中也有权限(当用户按下facebook登录按钮时,它位于onClick()内部: mFbLoginManager.logInWithReadPermissions(LoginActivity.this, Arrays.asList("user_photos", "email", "public_profile")

所以我不确定我的代码中存在什么问题。登录按钮是自定义的而不是facebook LoginButton,我不知道这是否重要。

欢迎提供每一个帮助

1 个答案:

答案 0 :(得分:3)

在Gradle中添加以下depandancy

implementation 'com.facebook.android:facebook-android-sdk:4.11.0'

在这里输入代码

 FacebookSdk.sdkInitialize(this);
    callbackManager =CallbackManager.Factory.create();

    // -----start putting in oncreate-----------------------

    LoginManager.getInstance().logInWithReadPermissions(UserActivity.this, Arrays.asList("public_profile", "user_friends","email"));

    facebookTime();

    //----onclicklisterner ---------------/

            callbackManager.onActivityResult(requestCode,resultCode,data);

    //---onactivityresult-------------/

    public void facebookTime() {
        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        System.out.println("Success");
                        GraphRequest request = GraphRequest.newMeRequest(
                                loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(JSONObject object, GraphResponse response) {
                                        //   Util.showCenteredToast(getActivity(), "object" + object);
                                        //     Util.showCenteredToast(getActivity(), "GraphResponse" + response);
                                        try {
                                            strEmail = object.getString("email");
                                            strUserName = object.getString("name");
                                            String id = object.getString("id");
                                            // write your code here
                                            //asyncTask.iSocialMediaResponse = LoginFragment.this;
                                            asyncTask.execute();


                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                        Bundle parameters = new Bundle();
                        parameters.putString("fields", "email,name");
                        //parameters.putString("fields", "user_friends");
                        request.setParameters(parameters);
                        //Log.e(" About to Graph Call", " ");
                        request.executeAsync();
                    }

                    @Override
                    public void onCancel() {
                        // App code
                        Util.showCenteredToast(UserActivity.this, "oncancel");
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        // App code
                        Util.showCenteredToast(UserActivity.this, "exception" + exception);
                    }
                });
    }

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

    }