Facebook AccessToken.getAccessToken is null on opening of app even after first login

空扰寡人 提交于 2019-11-29 06:20:13

The access token that was returned by the LoginManager will be saved in shared preferences, so the next time the app is opened, AccessToken.getCurrentAccessToken() should have the same access token, this is the same as how it with the Session class. You can check out the samples provided with the SDK to see them work.

Make sure you're not reinstalling the app between sessions, or setting the current access token to null explicitly.

You can also add an InitializeCallback to the sdkInitialize and check the AccessToken inside the callback:

FacebookSdk.sdkInitialize(getApplicationContext(), new FacebookSdk.InitializeCallback() {
        @Override
        public void onInitialized() {
            if(AccessToken.getCurrentAccessToken() == null){
                System.out.println("not logged in yet");
            } else {
                System.out.println("Logged in");
            }
        }
    });

I have not tested this myself but what I think you can do is to get the AccessToken from the LoginResult object in the onSuccess method of the callback.

So: AccessToken accessToken = loginResult.getAccessToken();

Then you would save this accessToken down by saying:

AccessToken.setAccessToken(accessToken);

Then when you call AccessToken.getAccessToken, it should return the accessToken you have saved.

Android Facebook sdk Initialization is take some time Initialize. so you need to wait just 100 milliseconds before calling AccessToken.getCurrentAccessToken()

once try this solution it is working for me ::

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

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {


            if (AccessToken.getCurrentAccessToken() != null) {
                Log.d(FBTAG, "facebook already logged in");
                isFBLogin = true;
            }
            LoginManager.getInstance().registerCallback(callbackManager,
                    new FacebookCallback<LoginResult>() {
                        @Override
                        public void onSuccess(LoginResult loginResult) {
                            // App code
                            Log.d(FBTAG, "facebook log in");
                            isFBLogin = true;
                        }

                        @Override
                        public void onCancel() {
                            // App code
                            isFBLogin = false;
                        }

                        @Override
                        public void onError(FacebookException error) {
                            isFBLogin = false;
                            Log.d(FBTAG, "facebook login error: " + error);
                            // App code

                        }
                    });


        }
    }, 100);
Rajesh Valappil

Please add the following code:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    //super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode,resultCode,data);
}

Reason is Facebook SDK profile caching.

    AccessToken.setCurrentAccessToken(null); 
    Profile.setCurrentProfile(null);

Before destroying activity try it. Maybe it can solve your problem. First time working it is clean cache second time you can login different account.

Pravin Patil

1.Change your function to this function

public void onClickLogin() {
    LoginManager.getInstance().logInWithReadPermissions(activity_name.this, PERMISSIONS);
}

Steps

1) FacebookSdk.sdkInitialize(getActivity().getApplicationContext());

2) CallbackManager mCallbackManager = CallbackManager.Factory.create();

    LoginManager.getInstance().registerCallback(mCallbackManager,
            new FacebookCallback<LoginResult>() {

                @Override
                public void onSuccess(LoginResult loginResult) {
                    //you will get access token here
                    mAccessToken = loginResult.getAccessToken();



                }

                @Override
                public void onCancel() {

                }

                @Override
                public void onError(FacebookException error) {

                }
            });

3) //get access token afterward by using AccessToken mAccessToken=AccessToken.getCurrentAccessToken();

Make Sure , If you use facebook login from fragment,

1)make sure you call super.onActivityResult() in fragment with Facebook callback manager.

2)check the super.onActivityResult() gets called in related "fragment activity" if you override onActivityResult in related activity.

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