Profile.getCurrentProfile() returns null after logging in (FB API v4.0)

前端 未结 4 1838
生来不讨喜
生来不讨喜 2020-12-08 06:44

For some reason Profile.getCurrentProfile() shows up null at times right after logging into FaceBook, using FB API v4.0.

This is causing problems for me

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 06:54

    I have a simpler suggestion than the other answers here.

    There is no need to register a login callback with:

    registerCallback(mCallbackManager, new FacebookCallback() { ... }
    

    Instead, whenever you need user data (like on button click) - start tracking profile changes with:

    new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile oldProfile, Profile profile) {
            stopTracking();
    
            Log.d(TAG, profile.getFirstName());
            Log.d(TAG, profile.getLastName());
            Log.d(TAG, String.format("https://graph.facebook.com/%s/picture?type=large", profile.getId()));
        }
    }.startTracking();
    

    And then start the Facebook login process by:

    LoginManager.getInstance().logInWithReadPermissions(getContext(), Arrays.asList("public_profile"));
    

    For the above code to work you of course need to prepare the usual stuff in your Activity:

    private CallbackManager mCallbackManager;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        mCallbackManager = CallbackManager.Factory.create();
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (FacebookSdk.isFacebookRequestCode(requestCode) &&
                mCallbackManager.onActivityResult(requestCode, resultCode, data)) {
            // do nothing
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
    

    Also do not forget to modify the AndroidManifest.xml file.

提交回复
热议问题