Facebook Login Not Returning User access_token

908 Views Asked by At

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

There are 3 best solutions below

4
On BEST ANSWER

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);
2
On

I never used the built-in facebook button ,instead i used the normal button with a click a listener. and i call this method in the onClick().

 myButton.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        //Starting facebook signIN
        startFbSignIn();
    }
});

startFbSignIn();

private void startFbSignIn() {

        List<String> permissions = new ArrayList<>();
        permissions.add("email");

        Session.openActiveSession(this, true, permissions, new Session.StatusCallback() {
            @Override
            public void call(final Session session, SessionState sessionState, Exception e) {

                Log.d("X", "Call Called");

                if (session.isOpened()) {

                    Log.d("X", "SessionOpened" + session.getAccessToken());

                    //Showing some loading


                    Request.newMeRequest(session, new Request.GraphUserCallback() {
                        @Override
                        public void onCompleted(GraphUser graphUser, Response response) {

                            //FACEBOOOK SIGNIN SUCCESS
                            Log.i("X", "Facebook Login Success");
                            //Log
                            Log.d("X", "GRAPH USER:" + graphUser.toString());


                            String name = graphUser.getName();
                            //.....

                        }
                    }).executeAsync();

                } else {
                    Log.d("X", "SessioNOTopened");
                }

            }
        });

    }

The above method successfully working for me and your onActivityResult() must look something like this

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    //Facebook Session Manager
    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
0
On

Probably cause of a bug in the facebook SDK you cannot directly get the accessToken. We need to create an accessToken tracker and then get the accessToken

    private AccessTokenTracker accessTokenTracker; 
    accessTokenTracker = new AccessTokenTracker() {


        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken) {
            try {

                if (newAccessToken.getToken() != null) {
                    facebookAccessToken = newAccessToken.getToken();
                    //this is your accessToken here

                }
                else if (oldAccessToken.getToken() != null) {
                    facebookAccessToken = oldAccessToken.getToken();
                     //this is your accessToken here
                }else {
                    Toast.makeText(getBaseContext(), "Facebook Login Failed", Toast.LENGTH_LONG).show();
                }

                if(oldAccessToken.getToken()!=null)
                Log.e("old", oldAccessToken.getToken());
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    };

once this is successfully done, overide the onStop method of the activity and make sure you stoptracking the accessToken there.

    accessTokenTracker.stopTracking();