Get Facebook User ID from Android SDK 4.0

How can I get Facebook User ID from Android Android SDK 4.0?

Here is my code that doesn't work:

protected void onCreate(Bundle savedInstanceState)

    CallbackManager cm;
    LoginButton     lb;

    cm = CallbackManager.Factory.create();
    lb = (LoginButton) findViewById(;
    Log.d(TAG, "Salut les zouzous");
            new FacebookCallback<LoginResult>()
                public void onSuccess(LoginResult loginResult)
                   Log.d(TAG, "Success !");
                    AccessToken tok;
                    tok = AccessToken.getCurrentAccessToken();
                    Log.d(TAG, tok.getUserId());

                public void onCancel()
                    Log.d(TAG, "On Cancel");

                public void onError(FacebookException exception)
                    Log.e(TAG, exception.getMessage());


I know for sure that

 Log.d(TAG, " Success"); 


does not print.

There is only the first log that prints something, outside of the callback.

Sorry for the bad english.


source to share

4 answers

Easier than



It is assumed that the user is already signed in.



You will need to do the following

        public void onSuccess(LoginResult loginResult) {
            final AccessToken accessToken = loginResult.getAccessToken();

            GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
                public void onCompleted(JSONObject user, GraphResponse graphResponse) {
                    Log.d(TAG, user.optString("email"));
                    Log.d(TAG, user.optString("name"));
                    Log.d(TAG, user.optString("id"));




just use loginResult.getAccessToken().getUserId()

in onSuccess () like

public void onSuccess(LoginResult loginResult) {

    Toast.makeText(Login.this, "User ID : "+loginResult.getAccessToken().getUserId(), Toast.LENGTH_LONG).show();





If you are using the login button that is provided by Facebook I think instead of


you need to use


This might explain why your callback isn't firing at all.

**** **** edit

If your callback still fails and you only need the user id, you can go instead of tracking the AccessToken.

Facebook offers an example where you override the onActivityResult method for an Activity / Fragment that you declared in a button:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);




All Articles