How to get FB User ID using Facebook Login Button in android application
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();
}
}
Comments
-
Anupam over 3 years
I'm developing an application in which I'm using
Facebook
log in button from SDK. I'm able to getaccess_token
of the user but I wantuserID
too. I have tried almost everything but was not successful. I have used facebook tutorial for achieving that. Here is the link: Facebook Login ButtonPlease help me in getting
userID
from the same login button.Any kind of help will be appreciated.
-
Ming Li about 11 yearsEven easier, the Request class has a newMeRequest helper method: developers.facebook.com/docs/reference/android/3.0/…
-
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 over 10 yearswhere 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 about 10 years
executeMeRequestAsync
is deprecated. UsenewMeRequest
instead -
RajaReddy PolamReddy almost 9 yearsit's a old facebook sdk . that time it has Facebook class in the facebook sdk
-
Ege Kuzubasioglu about 7 yearsfor some reason
Profile profile = Profile.getCurrentProfile();
andprofile.getUserId()
is not working with current sdk. So, this is the best solution currently.