How to get FB User ID using Facebook Login Button in android application

41,644

Solution 1

After successful login, you need to call following code. You can get the logged-in user details with id in response.

Session session = Session.getActiveSession();
Request request = Request.newGraphPathRequest(session, "me", null);
com.facebook.Response response = Request.executeAndWait(request)

Solution 2

Use the getId method of FB Profile class( Facebook SDK 4.0).

  Profile profile = Profile.getCurrentProfile();
  System.out.println(profile.getFirstName());
  System.out.println(profile.getId());

Solution 3

loginResult.getAccessToken().getUserId()

Solution 4

You should be used new facebook sdk 3.0 and

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
        if (Session.getActiveSession().isOpened()) {
            // Request user data and show the results
            Request.executeMeRequestAsync(Session.getActiveSession(), new Request.GraphUserCallback() {

                @Override
                public void onCompleted(GraphUser user, Response response) {
                    // TODO Auto-generated method stub
                    if (user != null) {
                        // Display the parsed user info
                        Log.v(TAG, "Response : " + response);
                        Log.v(TAG, "UserID : " + user.getId());
                        Log.v(TAG, "User FirstName : " + user.getFirstName());

                    }
                }

            });
        }
    }

Solution 5

Based on answer from @Ketan Mehta (https://stackoverflow.com/a/17397931/624109) that uses a deprecated API, you should use the following code:

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

    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);

    if (Session.getActiveSession().isOpened())
    {
        // Request user data and show the results
        Request.newMeRequest(Session.getActiveSession(), new GraphUserCallback()
        {
            @Override
            public void onCompleted(GraphUser user, Response response)
            {
                if (null != user)
                {
                    // Display the parsed user info
                    Log.v(TAG, "Response : " + response);
                    Log.v(TAG, "UserID : " + user.getId());
                    Log.v(TAG, "User FirstName : " + user.getFirstName());

                }
            }
        }).executeAsync();
    }
}
Share:
41,644
Anupam
Author by

Anupam

Android Developer

Updated on October 04, 2020

Comments

  • Anupam
    Anupam over 3 years

    I'm developing an application in which I'm using Facebook log in button from SDK. I'm able to get access_token of the user but I want userID too. I have tried almost everything but was not successful. I have used facebook tutorial for achieving that. Here is the link: Facebook Login Button

    Please help me in getting userID from the same login button.

    Any kind of help will be appreciated.

  • Ming Li
    Ming Li about 11 years
    Even easier, the Request class has a newMeRequest helper method: developers.facebook.com/docs/reference/android/3.0/…
  • PeteH
    PeteH almost 11 years
    #RajaReddy PolamReddy: how did you define the variable fb? And are you referring to the 3.0 SDK (or 2.0 with the deprecated Facebook class)?
  • alicanbatur
    alicanbatur over 10 years
    where will we start the activity ? You shared what we should do at the end, but could you please show me where to start intent?
  • Muzikant
    Muzikant about 10 years
    executeMeRequestAsync is deprecated. Use newMeRequest instead
  • RajaReddy PolamReddy
    RajaReddy PolamReddy almost 9 years
    it's a old facebook sdk . that time it has Facebook class in the facebook sdk
  • Ege Kuzubasioglu
    Ege Kuzubasioglu about 7 years
    for some reason Profile profile = Profile.getCurrentProfile(); and profile.getUserId() is not working with current sdk. So, this is the best solution currently.