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条回答
Bombasti
2楼-- · 2019-01-25 08:38

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.

查看更多
女痞
3楼-- · 2019-01-25 09:00

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.

查看更多
登录 后发表回答