Get Facebook Access Token in Share Dialog

I have two log in cases for Facebook. In first user logs in directly. In second user logs in for share dialog when not logged in. If not logged in and I try to share I am unable to get current access token. I have to log in AccessToken token = AccessToken.getCurrentAccessToken();. Token is null here. If I login LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "email", "user_friends")); and then try to share I can get the current access token.
How do I get the current access token when not logged in and using share dialog?
My question is whatever the case for log in is, call back should go to registerCallback? Well in case of log in before sharing it doesn’t!
Here is my code:

private void prepareFblogin()
{
    FacebookSdk.sdkInitialize(MainActivity.this);
    callbackManager = CallbackManager.Factory.create();


    LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            // App code
            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {
                            try {
                                AccessToken token = AccessToken.getCurrentAccessToken();
                                Log.v("FB Login", "Success:" + token.getToken());
                                Log.v("FB Login", "Success:" + object.toString());
                                Log.v("FB Login", "id:" + object.getString("id"));
                                Log.v("FB Login", "email:" + object.getString("email"));

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,email,name");
            request.setParameters(parameters);
            request.executeAsync();
        }

        @Override
        public void onCancel() {
            Log.v("FB Login", "cancel");
        }

        @Override
        public void onError(FacebookException e) {
            Log.v("FB Login", "Error");
        }
    });
}
public void prepareShareDialog(){
    shareDialog = new ShareDialog(this);
    // this part is optional
    shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
        @Override
        public void onSuccess(Sharer.Result result) {
            AccessToken token = AccessToken.getCurrentAccessToken(); // this returns null

        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException e) {

        }
    });


Source: facebook

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.