问题
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.
回答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)
回答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());
回答3:
loginResult.getAccessToken().getUserId()
回答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());
}
}
});
}
}
回答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();
}
}
回答6:
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.
回答7:
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");
回答8:
You can get the user ID with this code :
document.getElementById('status').innerHTML =
'Welcome ' + response.name + '!' + "<br />"+
'Your user ID is : ' + response.id;
来源:https://stackoverflow.com/questions/15568159/how-to-get-fb-user-id-using-facebook-login-button-in-android-application