Search code examples
androidfacebook-loginfacebook-android-sdkfacebook-access-tokenfacebook-rest-api

Facebook Login Not Returning User access_token


This is my first post, so please let me know if I'm not abiding by the community rules.

Now to the problem! I've been trying to setup the Facebook login process for an app and I've gotten as far as adding a GUI element (the built-in Facebook login button), and having the button go ahead and ask for a user/pass, granting the app access to the user's account. The problem I'm facing is that when using the following code to get a callback value from the app I receive nothing (no value being printed). Right now I'm printing to LogCat arbitrary values, however as soon as printing works, I look to access the user access_token and other information (which didn't initially work). Any help appreciated!

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_connect_accounts);

            Log.i("fromMe", "test1");
            FacebookSdk.sdkInitialize(this.getApplicationContext());
            callbackManager = CallbackManager.Factory.create();
            LoginButton loginButton = (LoginButton)findViewById(R.id.login_button);
            loginButton.registerCallback(callbackManager, new   FacebookCallback<LoginResult>(){

                public void onSuccess(LoginResult loginResult) {
                    Log.i("fromMe", "1");
                }

                public void onCancel() {
                    Log.i("fromMe", "2");
                }

                public void onError(FacebookException exception) {
                    Log.i("fromMe", "3");
                }
            });
        }

Solution

  • if Log.i("fromMe", "1");work so you can call :

    AccessToken accessToken = loginResult.getAccessToken(); 
    

    in your onSuccess().

    And in onActivityResult(int requestCode, int resultCode, Intent data) add :

    super.onActivityResult(requestCode, resultCode, data); 
    callbackManager.onActivityResult(requestCode, resultCode, data);