Facebook SDK: Why does Profile.getCurrentProfile() always return null for the first time?

扶醉桌前 提交于 2019-12-08 04:18:02

问题


I am implementing Facebook login in my Android app using LoginButton class provided in Facebook SDK. Everything kind of works after following instructions here. The only thing I am doing differently in my app, is that I have added the login button on a fragment as against the activity itself, to avoid polluting the login activity with a lot of code. But here's a problem: I want to get the user's public profile information. And I do that using Profile.getCurrentProfile() call. Here's the complete code:

// Called in onCreate:
    private void attemptLogin() {

            mCallbackManager = CallbackManager.Factory.create();
            FacebookCallback<LoginResult> mFacebookCallback = getFacebookCallback();
            mLoginButton.registerCallback(mCallbackManager, mFacebookCallback);
        }

And the mFacebookCallback code is this:

private FacebookCallback<LoginResult> getFacebookCallback() {
        return new FacebookCallback<LoginResult>() {

                @Override
                public void onSuccess(LoginResult loginResult) {

                    mProfileTracker = new ProfileTracker() {

                        @Override
                        protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {

                            if (currentProfile != null && oldProfile != null) {
                                mProfile = currentProfile;
                                // Get the new email and fire the home page of the activity, when email is available
                                requestUserEmail();
                            }

                            mProfileTracker.stopTracking();

                        }
                    };

                    mProfileTracker.startTracking();

                    if (null != loginResult.getAccessToken()) {

                        String userId = loginResult.getAccessToken().getUserId();

                        Log.d(TAG, "Successfully Logged-In to Facebook");
                        Toast.makeText(getActivity(), "Logged-In to Facebook:" + userId, Toast.LENGTH_SHORT).show();


                        mProfile = Profile.getCurrentProfile();

                        // Now that login is successful, email can be requested
                        requestUserEmail();
                    }

                }

                @Override
                public void onCancel() {
                    // App code
                    Log.d(TAG, "Cancelled Log-In to Facebook");
                    Toast.makeText(getActivity(), "Cancelled Log-In to Facebook", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onError(FacebookException exception) {
                    // App code
                    Log.d(TAG, "Error while Logging-In to Facebook");
                    Toast.makeText(getActivity(), "Error while Logging-In to Facebook", Toast.LENGTH_SHORT).show();
                }

            };
    }

I get AccessToken and even email just fine. But for the first installation of the app, Profile.getCurrentProfile() always returns null.

I also observed that onCurrentProfileChanged() is never really called, as I was expecting it to be called the first time the login attempt is made. I am not sure if onCurrentProfileChanged() related code should be within onSuccess(). But it seems people have had more luck by doing that, as per the question here.

What am I doing wrong here? What do I need to do to make sure that I get the profile information in the first go?


回答1:


After a few minutes of debugging I found out the obvious issue. In the code below:

if (currentProfile != null && oldProfile != null) {
    mProfile = currentProfile;
   // Get the new email and fire the home page of the activity, when email is available
   requestUserEmail();
}

The first time, oldProfile is always null. Therefore, mProfile is never set to current profile.

Secondly, I am not sure if it makes any difference, but changing

mLoginButton.setReadPermissions(Arrays.asList("user_friends", EMAIL));

to

mLoginButton.setReadPermissions(Arrays.asList("public_profile", "user_friends", EMAIL));

might have helped.

Also, just for the sake of completion, it is sufficient to request for email ( call to requestUserEmail() function in this case) only once in onCurrentProfileChanged is sufficient



来源:https://stackoverflow.com/questions/30425442/facebook-sdk-why-does-profile-getcurrentprofile-always-return-null-for-the-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!