How to get current Facebook access token on app start?

社会主义新天地 提交于 2019-12-10 19:16:31

问题


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 https://stackoverflow.com/a/29854249/1016472

My 2nd try was to use the AccessTokenTracker but this doesn't work too. To use the AccessTokenTracker I have to initialize the Facebook SDK first. But during this initialization the access token is send to the tracker. The tracker isn't created at this time and can't receive the token.

My 3rd try was to create my own AccessTokenTracker which looks like the original one. I created and registered it before I initialize the SDK. And now my tracker get's the token during this SDK initialization process.

With this knowledge I open a bug report at Facebook with the questions:

  • Why the AccessTokenTracker depends on the initialized SDK. I think this is a mistake.
  • What is the right way to see if a user is logged in or not on app start.

The answer from Facebook:

This doesn't look like bug. The AccessTokenTracker ::onCurrentAccessTokenChanged should let you know when the class has received a token.

I know that this is not working. And now I know there is no bug.

But how to solve my problem?

This doesn't work:

FacebookSdk.sdkInitialize(getApplicationContext());
mAccessTokenTracker = new AccessTokenTracker() {

    @Override
    protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {

        Timber.i("AccessTokenTracker oldAccessToken: " + oldAccessToken + " - currentAccessToken: " + currentAccessToken);
    }
};

回答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.




回答2:


It must be so (this Facebook v4.1.2)

FacebookSdk.sdkInitialize(getApplicationContext());

mFacebookCallbackManager = CallbackManager.Factory.create();

mFacebookAccessTokenTracker = new AccessTokenTracker() {
    @Override
    protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
        ...
    }
};

mFacebookLoginButton = new LoginButton(this);
mFacebookLoginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday"));
mFacebookLoginButton.registerCallback(mFacebookCallbackManager, new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        ...
        String token = loginResult.getAccessToken().getToken()
        ...
    }

    @Override
    public void onCancel() {
        Lo.d("Facebook login - the user did not give permission");
    }

    @Override
    public void onError(FacebookException e) {
        e.printStackTrace();
    }
});



回答3:


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

@Override
protected void onCreate(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();
    }
}

private void waitForFacebookSdk() {
    AsyncTask asyncTask = new AsyncTask() {
        @Override
        protected Object doInBackground(Object[] params) {
            int tries = 0;
            while (tries < 3) {
                if (AccessToken.getCurrentAccessToken() == null) {
                    tries++;
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    return null;
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object result) {
            init();
        }
    };
    asyncTask.execute();
}

private void init() {
    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.




回答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




回答5:


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




回答6:


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



来源:https://stackoverflow.com/questions/30379616/how-to-get-current-facebook-access-token-on-app-start

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