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

18,575

Solution 1

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.

Solution 2

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");
            }
        }
    });

Solution 3

Please add the following code:

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

Solution 4

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.

Solution 5

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);
Share:
18,575
Rohit Goyal
Author by

Rohit Goyal

Founder, CEO at PaxPlay Android Games | iOS Games LinkedIn | Twitter | AngelList

Updated on July 07, 2022

Comments

  • Rohit Goyal
    Rohit Goyal almost 2 years

    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.

  • AndroidDev
    AndroidDev almost 9 years
    But if I uninstall the app & reinstall it again & try to click the Login button, then it won't allow me to Login. Instead it called public void onError(FacebookException error) method, and show this exception "Invalid key hash. The key hash NDMi5aisFkTeU6BlER3L8JZ+wHw= does not match any stored key hashes"
  • Dante
    Dante almost 9 years
    @AndroidDev, that's an other issue. You will have to put that exact hash key in the valid hash keys section @ your app's page @ settings.
  • deRonbrown
    deRonbrown about 8 years
    There is a race condition if you are initializing the Facebook SDK in Application#onCreate() and checking that the current access token is not null in your MainActivity's onCreate(). In rare cases the user would appear logged out because I was checking the current access token less than 100 milliseconds before the Facebook SDK was initialized. For reference, the access token will always be null until this future task finishes executing: github.com/facebook/facebook-android-sdk/blob/…
  • Beto Caldas
    Beto Caldas almost 8 years
    Where do you found this information? I didn't find it into developers.facebook.com/docs/facebook-login/android/v2.2
  • Mr Special
    Mr Special over 5 years
    I face the same problem, it looks like you only explain the error @Gokhan Caglar. How can I fix this one ?
  • Jorgesys
    Jorgesys over 2 years
    Why 100 ms? where did you get this information?