Skip to content Skip to sidebar Skip to footer

How To Get Current Facebook Access Token On App Start?

On app start I need to know if the user is logged in or not to show a login page or not. My first try was to call AccessToken.getCurrentAccessToken(), but this doesn't work, see ht

Solution 1:

You need to initialize the SDK first as you said. Then create the access token tracker, and then check the current access token via AccessToken.getCurrentAccessToken().

If the SDK initializer is fast enough that the token is set before you can create the tracker, then your call of currentAccessToken will get the token. If the sdk initialization is slower than then tracker registration then the tracker will get the access token.

Solution 2:

It must be so (this Facebook v4.1.2)

FacebookSdk.sdkInitialize(getApplicationContext());

mFacebookCallbackManager = CallbackManager.Factory.create();

mFacebookAccessTokenTracker = newAccessTokenTracker() {
    @OverrideprotectedvoidonCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
        ...
    }
};

mFacebookLoginButton = newLoginButton(this);
mFacebookLoginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday"));
mFacebookLoginButton.registerCallback(mFacebookCallbackManager, newFacebookCallback<LoginResult>() {
    @OverridepublicvoidonSuccess(LoginResult loginResult) {
        ...
        String token = loginResult.getAccessToken().getToken()
        ...
    }

    @OverridepublicvoidonCancel() {
        Lo.d("Facebook login - the user did not give permission");
    }

    @OverridepublicvoidonError(FacebookException e) {
        e.printStackTrace();
    }
});

Solution 3:

@Gokhan Caglar is right, the access token is null if the Facebook Sdk is not done initializing. So what about this?

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(this/* applicationContext */);
    setContentView(R.layout.activity_getting_started);

    callbackManager = CallbackManager.Factory.create();

    if (AccessToken.getCurrentAccessToken() == null) {
        waitForFacebookSdk();
    } else {
        init();
    }
}

privatevoidwaitForFacebookSdk() {
    AsyncTask asyncTask = newAsyncTask() {
        @OverrideprotectedObjectdoInBackground(Object[] params) {
            int tries = 0;
            while (tries < 3) {
                if (AccessToken.getCurrentAccessToken() == null) {
                    tries++;
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    returnnull;
                }
            }
            returnnull;
        }

        @OverrideprotectedvoidonPostExecute(Object result) {
            init();
        }
    };
    asyncTask.execute();
}

privatevoidinit() {
    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    if (accessToken == null || accessToken.isExpired()) {
        // do work
    } else {
        // do some other work
    }
}

Not the best solution in my eyes but waiting a bit for the Facebook Sdk might help.

Solution 4:

Initialize Facebook SDK in the Intializer.java application so that any code in the activity will execute after the initialization. Add your Initializer to manifest file

Solution 5:

If you are login then you can write AccessToken.getCurrentAccessToken(); to get your current acess token

Post a Comment for "How To Get Current Facebook Access Token On App Start?"