Facebook AccessToken.getAccessToken is null on ope

2019-01-25 08:18发布

I have integrated latest Facebook android sdk (Sdk 4.0). This is the code I have added in my onCreate method.

FacebookSdk.sdkInitialize(this.getApplicationContext());
            callbackManager = CallbackManager.Factory.create();
            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

                        }
            });

And this is the code I have used for onClickLogin

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

I am able to login by clicking on the login button and processing onClickLogin function. Now next time I am opening the app the app I am checking for AccessToken.getAccessToken to check if the user is already logged in at facebook but it is always coming as null. Isn't there anyway in the new sdk to login in the background so that I don't have to ask the user to login always like it used to be in the previous version in session class.

8条回答
霸刀☆藐视天下
2楼-- · 2019-01-25 08:33

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);
查看更多
爷的心禁止访问
3楼-- · 2019-01-25 08:33

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.

查看更多
聊天终结者
4楼-- · 2019-01-25 08:34

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");
            }
        }
    });
查看更多
▲ chillily
5楼-- · 2019-01-25 08:36

Please add the following code:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    //super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode,resultCode,data);
}
查看更多
贪生不怕死
6楼-- · 2019-01-25 08:37

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.

查看更多
在下西门庆
7楼-- · 2019-01-25 08:37

1.Change your function to this function

public void onClickLogin() {
    LoginManager.getInstance().logInWithReadPermissions(activity_name.this, PERMISSIONS);
}
查看更多
登录 后发表回答