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
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());
}
}
});
}
}
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());
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)
The Facebook graph API lets you make calls that return JSON, which you can parse in Android with a JSONObject like this below code.
JSONObject me = new JSONObject(fb.request("me"));
String id = me.getString("id");
As these all answers are for older SDK , for SDK 4.0 here is the code
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
Profile profile = Profile.getCurrentProfile();
Log.d("facebook",profile.getId());
}
@Override
public void onCancel() {
// App code
Log.d("facebook","failed");
}
@Override
public void onError(FacebookException exception) {
// App code
Log.d("facebook","failed");
}
});
i was searching for the same thing looks like the Session object is no more accessible so above is the new way to get user profile and user id.
You can get the user ID with this code :
document.getElementById('status').innerHTML =
'Welcome ' + response.name + '!' + "<br />"+
'Your user ID is : ' + response.id;