Jidey
Jidey

Reputation: 359

Get Facebook user ID with android SDK 4.0

How can I get the Facebook User ID with the Facebook Android SDK 4.0?

Here is my code that doesn't work:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_firstlogin);

    CallbackManager cm;
    LoginButton     lb;

    cm = CallbackManager.Factory.create();
    lb = (LoginButton) findViewById(R.id.facebook_button);
    Log.d(TAG, "Salut les zouzous");
    LoginManager.getInstance().registerCallback(cm,
            new FacebookCallback<LoginResult>()
            {
                @Override
                public void onSuccess(LoginResult loginResult)
                {
                   Log.d(TAG, "Success !");
                    AccessToken tok;
                    tok = AccessToken.getCurrentAccessToken();
                    Log.d(TAG, tok.getUserId());
                }

                @Override
                public void onCancel()
                {
                    Log.d(TAG, "On Cancel");
                }

                @Override
                public void onError(FacebookException exception)
                {
                    Log.e(TAG, exception.getMessage());
                }
            });

I precise that the

 Log.d(TAG, " Success"); 

doesn't print.

There is only the first log who print something, outside the callback.

Sorry for the bad English.

Upvotes: 8

Views: 17788

Answers (4)

Hafiz Muneeb
Hafiz Muneeb

Reputation: 307

just use loginResult.getAccessToken().getUserId() in onSuccess() as

@Override
public void onSuccess(LoginResult loginResult) {

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

}

Upvotes: 6

Markymark
Markymark

Reputation: 3009

As simple as

Profile.getCurrentProfile().getId()

This assumes the user is already logged in.

Upvotes: 31

Keab42
Keab42

Reputation: 688

If you're using the Login Button that is provided by Facebook then I think instead of

LoginManager.getInstance().registerCallback()

you need to use

lb.registerCallback()

That could explain why your callback isn't firing at all.

****edit****

If your callback's still aren't firing and you only need the UserID you could go for AccessToken Tracking instead.

Facebook offers an example where you override the onActivityResult method of the Activity/Fragment that you've declared the button in:

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

Upvotes: 0

Murtaza Khursheed Hussain
Murtaza Khursheed Hussain

Reputation: 15336

You will need to do the following

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

            GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
                @Override
                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"));
                }
            }).executeAsync();
        }

Upvotes: 9

Related Questions