Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Access Token always null

I have some code in my main Activity which calls My facebook Login activity class if AccessToken.getCurrentAccessToken() is null:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.Profile;
import com.facebook.ProfileTracker;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;

import java.util.Arrays;

import matthewboyle.lurker_android.MainFeedScreen;
import matthewboyle.lurker_android.utilities.ConnectionChecker;

/**
 * Created by matthewboyle on 28/05/2017.
 */

public class FacebookLogin extends AppCompatActivity {
    private CallbackManager mCallbackManager;
    private AccessTokenTracker accessTokenTracker;
    private ProfileTracker profileTracker;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mCallbackManager = CallbackManager.Factory.create();
        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
                Log.d("facebook", "onCurrentAccessTokenChanged");


            }
        };
        profileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
                Log.d("facebook", "onCurrentProfileChanged");

            }
        };
        accessTokenTracker.startTracking();
        profileTracker.startTracking();

        LoginManager.getInstance().registerCallback(mCallbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        Log.d("facebook", "in on success");
                        AccessToken accessToken = loginResult.getAccessToken();
                        Log.d("permissions", accessToken.getPermissions().toString());

                        Log.d("facebook", "in on success,got a token and its "+accessToken);

                    }

                    @Override
                    public void onCancel() {
                        Log.d("facebook", "in cancel");

                    }

                    @Override
                    public void onError(FacebookException exception) {
                        Log.d("facebook", "in error");
                        Log.d("facebook", exception.toString());




                    }


                });
        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_posts","user_likes","user_about_me","user_managed_groups","user_tagged_places"));
        Log.d("facebook", "Done so redirecting with "+AccessToken.getCurrentAccessToken());

        startActivity(new Intent(FacebookLogin.this,MainFeedScreen.class));


    }
    @Override
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
        super.onActivityResult(requestCode, responseCode, intent);
        //Facebook login
        mCallbackManager.onActivityResult(requestCode, responseCode, intent);

    }
}

If I run this code, the only print statement I get is:

 Done so redirecting with null

It doesn't seem to hit any other method in the class at all.

It is worth mentioning that this code worked fine until I reinstalled the app. I went onto Facebook and updated the hash key in my app but that didn't seem to help. However, I'm not seeing any errors due to hash key.

Would appreciate any help.

like image 350
Matt Boyle Avatar asked Jul 12 '17 17:07

Matt Boyle


People also ask

How do I check if my Facebook access token is valid?

You can simply request https://graph.facebook.com/me?access_token=xxxxxxxxxxxxxxxxx if you get an error, the token is invalid. If you get a JSON object with an id property then it is valid. Unfortunately this will only tell you if your token is valid, not if it came from your app.

What does access token mean on Facebook?

An access token is an opaque string that identifies a user, app, or Page and can be used by the app to make graph API calls. When someone connects with an app using Facebook Login and approves the request for permissions, the app obtains an access token that provides temporary, secure access to Facebook APIs.


2 Answers

You need to move the "startActivity" line inside your "onSuccess" callback like this:

LoginManager.getInstance().registerCallback(mCallbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    Log.d("facebook", "in on success");
                    AccessToken accessToken = loginResult.getAccessToken();
                    Log.d("permissions", accessToken.getPermissions().toString());

                    Log.d("facebook", "in on success,got a token and its "+accessToken);

                    Log.d("facebook", "Done so redirecting with "+AccessToken.getCurrentAccessToken());

                    startActivity(new Intent(FacebookLogin.this,MainFeedScreen.class));

                }

                @Override
                public void onCancel() {
                    Log.d("facebook", "in cancel");

                }

                @Override
                public void onError(FacebookException exception) {
                    Log.d("facebook", "in error");
                    Log.d("facebook", exception.toString());




                }


            });
like image 180
Duda Avatar answered Oct 09 '22 21:10

Duda


I am using Fb SDK 4.1 and this code snippet is working for me :

And as @Duda mentioned You need to move the "startActivity" line inside your "onSuccess" callback

@Override
public void onSuccess(LoginResult loginResult) {

    AccessToken accessToken = loginResult.getAccessToken();

    Log.d("facebook", "Done so redirecting with "+accessToken.getToken());

    startActivity(new Intent(FacebookLogin.this,MainFeedScreen.class));

}
like image 39
Nilesh Deokar Avatar answered Oct 09 '22 21:10

Nilesh Deokar