Android Facebook API - I receive emails only for some fb accounts - android

I have been using the Facebook SDK for Android a bit now. I have been having trouble where one of my testers could not create an account in my app using Facebook. After some debugging I figured out that for some reason when he tries to login / register the JSON object that contains the information does not come with his email. This only happens for HIS account!
Does anyone know why this could happen?
(I have included a simplified version of my login code so you can see if there is anything wrong with it that might cause it)
List<String> permissionNeeds = Arrays.asList("public_profile", "user_friends", "email");
loginButton.setReadPermissions(permissionNeeds);
loginButton.registerCallback(mCallbackManager, 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) {
try {
Log.d(TAG, object.toString()); // Has no email sometimes!
} catch (JSONException e) {
e.printStackTrace();
Log.e(TAG, String.format("Error with facebook login: %s", e.getMessage()));
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,picture,email,gender");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
Log.d(TAG, String.format("Error with facebook login: %s", error.getMessage()));
}
});
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(SplashActivity.this,
Arrays.asList("public_profile", "user_friends", "email"));
}
});
If any more information is needed, please tell me I'll be happy to add some.
Thanks!

So turns out that my tester had an inactive email account.
In general you should never be counting on every Facebook user having an email address.

Related

Facebook auth with firebase get User data

I am trying to use Firebase authentication with Facebook.
I have successfully implemented the login part and stuck while getting the user details from facebook using accessToken.
Here is the code:
loginButton.setReadPermissions("email", "public_profile" );
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject me, GraphResponse response) {
Log.e("FBRESPONSE",me.toString());
if (response.getError() != null) {
// handle error
} else {
if (me.has("picture")) {
try {
f_photo = me.getJSONObject("picture").getJSONObject("data").getString("url");
} catch (JSONException e) {
e.printStackTrace();
}
}
f_email = me.optString("email");
String id = me.optString("id");
f_name=me.optString("first_name")+" "+me.optString("last_name");;
handleFacebookAccessToken(loginResult.getAccessToken());
}
}
}).executeAsync();
}
});
This is the JSON response i get when called this.
E/FBRESPONSE: {"name":"Vishnu Reddy","id":"855235691328214"}
How do i get email, profile picture URL?
Please help. Thanks in advance.
You need to use the Facebook Graph API. You need to have your user signed in and call the AccessToken.refreshCurrentAccessTokenAsync(); and initiate a request to the user data-fields you need.

How to get signed in Facebook user id number in 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()

Facebook shows two separate login dialogs and fails login

I am logging into facebook on Android using facebook android sdk (4.7.0). Most of the time it shows following dialog and works fine.
But sometimes it shows the following dialog and login does not work.
What are possibilites for failed login in this case? I am testing it on android (5.1.1).
facebookButton.setReadPermissions(Arrays.asList("public_profile, email, user_birthday"));
callbackManager = CallbackManager.Factory.create();
facebookButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
try {
fbid = object.getString("id");
fbfirst_name = object.getString("first_name");
fblast_name = object.getString("last_name");
fbemail = object.getString("email");
new FBRegistrationService().execute();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday,first_name,last_name");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException exception) {
}
});
Update: I am also getting these logs.
"Blink deferred a task in order to make scrolling smoother. Your timer and network tasks should take less than 50ms to run to avoid this. Please see https://developers.google.com/web/tools/chrome-devtools/profile/evaluate-performance/rail and https://crbug.com/574343#c40 for more information.", source: https://m.facebook.com/login.php?skip_api_login=1&api_key=350893648453711&signed_next=1&next=https%3A%2F%2Fm.facebook.com%2Fv2

Why Facebook user details are not coming?

I am doing simple facebook login using latest facebook sdk 4.8 but after login i am getting null value no user details are coming but only i am getting the details if the user is developer and administrator of that Facebook App.
And also i have given facebook permission.
loginButton.setReadPermissions(Arrays.asList("public_profile", "user_likes",
"user_friends", "email", "user_birthday", "user_photos", "user_about_me"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
try{
nameTxt.setVisibility(View.VISIBLE);
nameTxt.setText(jsonObject.getString("name"));
}catch(Exception e){
e.printStackTrace();
Log.d("FacebookException", e.toString());
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,gender,birthday,email,bio,photos{link}");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
});
Have you switched your app status to live? You can see status in your app's Dashboard in developers console.
If not, just switch status to live in Status & Review tab. This switch should be switched to 'Yes' position

Post Id facebook share dialog always return null in Android

I used test app id and log on by test user create at dash_board app on facebook develop site, require pulish_actions permission when login using login button widget of facebook sdk but result get postid always = null.
Here is my code:
....
shareDialog = new ShareDialog(MainActivity.this);
shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
if (result.getPostId() != null)
Log.e(TAG, result.getPostId());
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
});
pulishButton.setOnClickListener(this);
try{
loginButton.setPublishPermissions(new String[]{"publish_actions","publish_stream"});
}catch (FacebookException e){
e.printStackTrace();
}
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.e(TAG, "success");
loginButton.setEnabled(false);
pulishButton.setEnabled(true);
GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject json, GraphResponse response) {
if (response.getError() != null) {
// handle error
System.out.println("ERROR");
} else {
System.out.println("Success");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}).executeAsync();
}
#Override
public void onCancel() {
Log.e(TAG, "On cancel");
}
#Override
public void onError(FacebookException e) {
Log.d(TAG, e.toString());
}
});
The solution is to force ShareDialog to use Feed mode and avoid using FB's App for sharing:
shareDialog.show(linkContent, ShareDialog.Mode.FEED);
I believe this is a bug by FB. They don't send postId to onSuccess callback when using FB App for sharing (postId = null), but they do if you use Feed.
I've heard you can avoid the "postId=null" problem by using Facebook Login and demanding "publish_actions" permission. But I don't think this is the correct way to deal with this problem. Regarding the mentioned permission Facebook states:
Publishing via dialogs or social plugins does not require this permission. Do not request review of this permission if you're only using Share dialog, Feed Dialog, Message Dialog etc, or Social Plugins (e.g. the Like Button.)
Facebook's docs on Share Dialog:
This does not require Facebook Login or any extended permissions, so it is the easiest way to enable sharing on the web.
Facebook confirmed this issue in their latest update:
https://developers.facebook.com/support/bugs/478167629274681/
https://developers.facebook.com/support/bugs/647119912303459/
You can't login directly with "publish_action", you have to first login with any read permission then ask for for publish permissions
https://developers.facebook.com/docs/facebook-login/permissions/v2.4#publishing

Categories

Resources