无法从Facebook SDK检索数据

时间:2016-06-08 12:21:11

标签: android facebook facebook-graph-api android-studio exception

任何想法,为什么我无法从Facebook收到电子邮件?我得到了错误 - 没有价值"电子邮件"或者那样的。

    private void fbInit() {
    // Initialize the SDK before executing any other operations,
    FacebookSdk.sdkInitialize(getApplicationContext());
    AppEventsLogger.activateApp(this);
    callbackManager = CallbackManager.Factory.create();

    // get new instance of fb
    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    loginResult.getAccessToken().getUserId();

                    getFbData(loginResult.getAccessToken().getUserId());
                }

                @Override
                public void onCancel() {

                }

                @Override
                public void onError(FacebookException error) {

                }
            });

    // access token tracker init
    accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(
                AccessToken oldAccessToken,
                AccessToken currentAccessToken) {
            // Set the access token using
            // currentAccessToken when it's loaded or set.
            accessToken = currentAccessToken;
            updateWithToken(currentAccessToken);
        }
    };

    // If the access token is available already assign it.
    accessToken = AccessToken.getCurrentAccessToken();
    updateWithToken(accessToken);

    // is this need?!
//        profileTracker = new ProfileTracker() {
//            @Override
//            protected void onCurrentProfileChanged(
//                    Profile oldProfile,
//                    Profile currentProfile) {
//                // App code
//            }
//        };
}

private void updateWithToken(AccessToken currentAccessToken) {
        // this way I check if there is email permission which is needed to register/login
        if (currentAccessToken != null
                && !currentAccessToken.getDeclinedPermissions().contains("email")
                && !currentAccessToken.getDeclinedPermissions().contains("basic_info")) {
            // go to other activity (?)
        } else {
            ActivityUtils.showToast(this, "no email permission");
        }
    }

private void getFbPermissions() {
    Collection<String> permissions = Arrays.asList("public_profile", "email", "user_photos", "user_birthday");
    LoginManager.getInstance().logInWithReadPermissions(this, permissions);
}

private void getFbData(String userId) {
    GraphRequest request = GraphRequest.newMeRequest(
            AccessToken.getCurrentAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {
                    if (response != null) {
                        try {
                            Log.i("LoginActivityFbResponse", response.toString());
                            ActivityUtils.showToast(getActivity(),
                                    object.getString("id") + object.getString("name") + object.getString("email")
                                            + object.getJSONArray("location").getString(1) + object.getString("locale")
                                            + object.getString("birthday") + object.getString("gender"));

                            // TODO: try to login
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id, name, email");
    request.setParameters(parameters);
    request.executeAsync();
}

顺便说一句。我想从Facebook获得至少这些数据:电子邮件,身份证,性别,生日,location_name。

1 个答案:

答案 0 :(得分:0)

生日时出现错误,因为您的GraphRequest未获得生日许可。 使用它像:

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

你可以尝试一下并得出结果吗?