How to get signed in Facebook user id number in Android - android

In Android I wonder how to get a Facebook user id number after he has signed in to my app. I tried this:
com.facebook.Profile.getCurrentProfile().getId()
But for some reason that give me a number but it´s not the user id.
When I go to sites like https://findmyfbid.com/ I get the correct id like "23982739223233". Even when I right click my Facebook profile picture in web browser and copy link the "referrer_profile_id=23982739223233" in the link is correct.
But when I sign in using that Facebook this Profile.getCurrentProfile().getId() gives me a completely different number

You can use GraphRequest
in code:
List<String> permissionNeeds = Arrays.asList(
"public_profile", "email", "user_birthday", "user_friends");
loginButton.setReadPermissions(permissionNeeds);
loginButton.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
try {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
// Application code
try {
Log.v("FACEBOOK LOGIN", response.toString());
String fb_id = object.getString("id"); //FaceBook User ID
String fb_name = object.getString("name");
String fb_email = object.getString("email");
String profilePicUrl = "https://graph.facebook.com/" + fb_id + "/picture?width=200&height=200";
} catch (Exception e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,picture.type(small)");
request.setParameters(parameters);
request.executeAsync();
} catch (Exception e) {
Log.d("ERROR", e.toString());
}
}
#Override
public void onCancel() {
Log.d("FACEBOOK ERRROR", "cancelled");
}
#Override
public void onError(FacebookException exception) {
Log.d("FACEBOOK ERRROR", exception.toString());
}
});
In Graph Request Response you can get user's Facebook Id, name , picture and other relevant information

ok I found the Answer. Looks like there is no common id for a Facebook user but my app get assigned a unique id as soon as user accept my app. Also here
This id is what i see with
com.facebook.Profile.getCurrentProfile().getId()

Related

Graph api. Not getting email field. Any other way of getting login username or email id?

I am using the graph API. I used below code for getting email id. But email id is returning as empty. Email-id is verified and is able to login with the testing account. Did I make any mistake? Permissions have been accepted.
login(){
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "public_profile"));
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("DEBUG",loginResult.getRecentlyDeniedPermissions()+" ACCEPTED PERMISSIONS :" +loginResult.getRecentlyGrantedPermissions());
Log.v("DEBUG",response.toString());
try {
String email = object.getString("id");
String name = object.getString("name");
LoginManager.getInstance().logOut();
sessionLogin(email, name, "Facebook");
} catch (JSONException e) {
Log.e(TAG, "facebook onCompleted exception: ", e);
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,first_name,name,last_name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}

Does anyone know how to get firstname, lastname, email, id, birthday, gender, hometown, age and etc from facebook sdk in android platform?

I am new for facebook sdk.
Does any know how to get firstname, lastname, email, id, birthday, gender, hometown, age and etc from facebook sdk in android platform?
Please show me the code?
below is what I have tried
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions( "public_profile", "email", "user_birthday", "user_friends");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
boolean isLoggedIn = accessToken != null && !accessToken.isExpired();
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
// Insert your code here
try {
if (object != null) {
JSONObject obj = response.getJSONObject();
id = obj.getString("id");
name = obj.getString("name");
birthday = obj.getString("birthday");
email = obj.getString("email");
String gender = obj.getString("gender");
showUserInfoToast();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,email,first_name,last_name,gender");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
I tried but it does not work....
please help
Please try this code .Hope it will fetch all the data you needed.Thanx
public void onSuccess(final LoginResult loginResult) {
Log.e("bug", "facebook sign in success");
if (loginResult != null) {
Profile profile = Profile.getCurrentProfile();
if (profile != null) {
firstName = profile.getFirstName();
lastName = profile.getLastName();
social_id = profile.getId();
profileURL = String.valueOf(profile.getProfilePictureUri(200, 200));
Log.e(TAG, "social Id: " + profileURL);
}
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.e("bug", "facebook social_id=>" + social_id + " toeken" + loginResult.getAccessToken().getToken());
Log.e("bug", "graph response=>" + object.toString());
try {
if (object.has("name")) {
String[] name = object.getString("name").split(" ");
firstName = name[0];
lastName = name[1];
}
email = object.has("email") ? object.getString("email") : "";
social_id = object.has("id") ? object.getString("id") : "";
socialSignUp("facebook");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,first_name,name,last_name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}
The data you access depends on the permissions someone grants your app and data they chose to share with apps (Link)
These fields can be null
To get any user-specific data other than userId, nameand email you have to submit your app for review to facebook console so that facebook will have clarity that what you want to do with user personal data.
If it is some sort of useful thing your app is doing with this data then facebook will allow you to access that data like birthday date, user_friends and user gender.
To do that go to facebook developer console and follow there procedure.
Hope this will help you to achieve what you want to achieve.

Not able to get the gender in graph api in android

I am not able to get the gender here in OnCompleted function
I am able to get other params like id,name,email
Currently app is in Dev mode
This was working yesterday.
Code:
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("LoginActivity", response.toString());
try {
AccessToken token = AccessToken.getCurrentAccessToken();
String accessToken = token.getToken().toString();
String expiresAt = token.getExpires().toString();
String id = object.getString("id");
String gender = object.getString("gender");
} catch (JSONException e) {
e.printStackTrace();
System.out.println(e.toString());
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, name, email, gender, location, picture.type(large)");
request.setParameters(parameters);
request.executeAsync();
In your https://developers.facebook.com/ account, under App Review section, click Start a Submission, select user_gender from LOGIN PERMISSIONS and go through submission steps.
After facebook will approve user_gender permission, you can use it.
Again in your developer account click Settings then Advanced and in Upgrade API Version
change the API version your app calls to v3.1 and using below code, you will get the gender of the user.
private void attemptToLogIn() {
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "user_gender"));
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
makeGraphRequest(loginResult.getAccessToken());
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
});
}
public void makeGraphRequest(AccessToken token) {
GraphRequest request = GraphRequest.newMeRequest(
token, (object, response) -> {
try {
String gender = object.getString("gender");
} catch (JSONException e) {
e.printStackTrace();
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "name,gender");
request.setParameters(parameters);
request.executeAsync();
}
Your access token need the user_gender permission:
https://developers.facebook.com/docs/facebook-login/permissions/v3.0#reference-user_gender

Profile.getCurrentProfile() return only my profile, all the rest are null

I'm trying to build a simple login to facebook app (with FB api v4.0)and retrieve some data about the user, but for some reason I success to login and get data only from my personal facebook profile, if tried to login with other users profile I'm getting null. (I change the setting in facebook developer to public app).
this is some of my loginButton_Fragment:
private CallbackManager callbackManager;
private FacebookCallback<LoginResult> facebookCallback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
Log.d("koko",accessToken.toString() );
if (profile != null){
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
try {
user_birthDay = object.getString("birthday");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,birthday");
request.setParameters(parameters);
request.executeAsync();
user_name = profile.getName();
user_id = profile.getId();
Log.d("koko","first " +user_name + " " + user_id + " " + user_birthDay );
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
comm.respone(user_name, user_id, user_birthDay);
}
}, 500);
}
}
#Override
public void onCancel () {
}
#Override
public void onError (FacebookException e){
}
};
Each time I login, I'm getting inside the onSuccess, but if its not my personal profile so the profile its null.
set read permissions mButtonLogin.setReadPermissions(Arrays.asList("email,public_profile","user_birthday"));
try like this
mButtonLogin.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d("status", "onSuccess");
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
try {
String name = object.getString("name");
String email =object.getString("email");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "name,email,birthday");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
Log.d("status", "onCancel");
}
#Override
public void onError(FacebookException e) {
Log.d("status", "onError " + e);
}
}
);
You need to acquire app specific permission from Facebook for retrieve user personal information's such as birthday, user_friends etc by submitting it for a review.
Invest few moments here .
If your app requests such permissions Facebook will have to review how your app uses it.
You will get public_profile as it's default .
Check this & this .

Facebook Android login get users email address

I'm using the Facebook registerCallBack method to get the current users information. The problem i am having is while trying to retrieve the email address.
// Callback registration
FBLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
final Profile profile = Profile.getCurrentProfile();
if (profile != null) {
}
//Toast.makeText(FacebookLogin.this,"Wait...",Toast.LENGTH_SHORT).show();
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
String email_id = object.getString("email");
String gender = object.getString("gender");
String facebook_id=profile.getId();
String f_name=profile.getFirstName();
String m_name=profile.getMiddleName();
String l_name=profile.getLastName();
String full_name=profile.getName();
String profile_image=profile.getProfilePictureUri(400, 400).toString();
Log.i("facebook_id",facebook_id);
Log.i("f_name",f_name);
Log.i("m_name",m_name);
Log.i("l_name",l_name);
Log.i("full_name",full_name);
Log.i("profile_image",profile_image);
Log.i("gender",gender);
Log.i("email_id",email_id+"");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday, friends,albums");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
Using this code i get the following log:
org.json.JSONException: No value for email
I have made sure that the email is publicly available in case that was an issue as i had a similar problem with the Google Plus login script.
Based on my comment above, when you get the differents fields:
parameters.putString("fields", "id, name, email, gender, birthday, friends, albums");
you certainly forgot the right permissions. Indeed, you probably set "user_profile" as follows:
loginButton.setReadPermissions("user_profile");
However, when you look at the reference documentation, you can see that "email" is a permission on its own, it's not included in "user_profile".
Therefore you have to set different permissions, included "email" as well, when you initiate the LoginManager (see Add LoginButton section). Instead of getting only one permissions, you can set an array as:
loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("user_profile", "email", "...", "..."));
Thanks to this, you will be able to retrieve the email address.

Categories

Resources