Facebook Android SDK — no 'email' in meRequest - android

I'm using the following code to get user's email:
AccessToken accessToken = AccessToken.getCurrentAccessToken();
GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
// Obtaining
}
}).executeAsync();
The following is checked via debugger:
AccessToken instance contains permissions "email" and "public_profile"
AccessToken instance declined permissions set is empty
Resulting JSONObject contains only user id and name, but no email
Profile has email — I'm authenticating via email.
I'm using Facebook SDK 4.5
EDIT: Request via direct HTTP graph api call returns email. But Android SDK does not.
How to fix it?

Try this out, you need to send "email" as part of parameters for GraphRequest.
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);
request.executeAsync();

I think the user can specify no email permissions when he/she signs in via facebook. So even if you expect it, you might not get the email address.

Maybe we facing same problem, try this :
Bundle params=new Bundle();
params.putString("fields","id,name,email,verified"); //must specify the "fields" param, otherwise FB always return id and name only.
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me",
params,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
//do something...
}
}
).executeAsync();

Related

Facebook Graph API not returning Gender

I am using Facebook SDK to sign-in a user. To get the gender I have added user_gender permission in the permission list.
List<String> accessPermissions = Arrays.asList("user_gender", "email", "user_birthday");
LoginManager.getInstance().logInWithReadPermissions(activity, accessPermissions);
After which I get the access token. Now I am passing this access token to the backend team. And the backend team is using Facebook Graph API to get the user details like birthday, gender, email, etc.
They hit the following API to get user details.
https://graph.facebook.com/me?access_token=dummyaccesstoken&fields=name,gender,birthday,email
They are able to get details like email and birthday. But they are not able to get the gender of the user. Since it is a GET API, I can also see that we are not getting the gender of the user.
However, if we try to get the details in Android itself instead of passing the access token to the backend, then it works fine.
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
Log.d("Response", "response is "+response.getJSONObject().toString());
// Application code
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link, gender");
request.setParameters(parameters);
request.executeAsync();
So what is the issue in the Facebook Graph API? Why is it not able to return the gender? Anyone else faced the issue?
List<String> accessPermissions = Arrays.asList("user_gender", "email", "user_birthday");
LoginManager.getInstance().logInWithReadPermissions(activity, accessPermissions);
write instead of user_gender and email and user_birthday
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email"));
and write the Bundle request params
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();

Get All Photos from Facebook

I need to get user uploaded and tagged photos from facebook account using graph API.
I tried following code but it's give nothing to me .
GraphRequest request = GraphRequest.newMeRequest(
token,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
// Insert your code here
Log.e("Hi "," "+response.getJSONObject().toString());
}
});
GraphRequest req = GraphRequest.newGraphPathRequest(token, "me/photos", callback);
Bundle parameters = new Bundle();
parameters.putString("fields", "id, name");
req.setParameters(parameters);
req.executeAsync();
I have given following permission to facbook.
loginButton.setReadPermissions(Arrays.asList("public_profile","user_photos"));
Thanks
Use this API
json.FB_URL+json.PAGE_ID+"?fields=albums.limit(30)%7Bname%2Cid%2Cdescription%2Cpicture%7D&access_token="+json.ACCESS_TOKEN

Set Facebook graph Request to always return English results

The language of the results returned depends on the settings on a users phone and/or their location. I want to force the request to only return English results.
The request in question is just a simple "me" request for a user.
So far I have tried to add "local" in the parameteres like this:
GraphRequest graphRequest = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
}
});
Bundle param = new Bundle();
param.putString("fields", "albums, id, birthday, photos&locale=en_us");
graphRequest.setParameters(param);
graphRequest.executeAsync();
I have also tried to do it like this:
String graphPath = "/me?fields=id,name , first_name,albums, last_name&locale=en_us";
GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), graphPath, null, HttpMethod.GET, new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse response) {
Log.i(TAG, "response: " + response);
Log.i(TAG, "object: " + response.getJSONObject());
}
});
GraphResponse response = request.executeAsync();
Both without any luck. Last method actually works in the Facebooks Graph Api Explorer: https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=me%3Ffields%3Dalbums%26locale%3Dnb_no&version=v2.5
Without adding the "&locale=en_us" the top graph request works as intended.
It was actually a bug in the Facebook sdk version I had, so it would not return only english results. It works to set "locale" as an extra parameter. The correct way to do it is:
GraphRequest graphRequest = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
}
});
Bundle param = new Bundle();
param.putString("fields", "gender, email, name, albums");
param.putString("locale", "en_US");
graphRequest.setParameters(param);
graphRequest.executeAsync();
If this does not work for you, try to upgrade your Facebook sdk. I'm now on version 'com.facebook.android:facebook-android-sdk:4.11.0' and this works perfectly.

Facebook SDK 4.5.0 returning wrong user id in android

In my App i am integrating facebook SDK 4.5.0.
To fetch details of user I am using method
GraphRequest request = GraphRequest.newMeRequest
In user json response i am getting wrong user id.
In case of IOS for same application and same user i get correct userId.
Can anyone help me to solve this?
This is my Code to fetch user details:
GraphRequest request = GraphRequest.newMeRequest(com.facebook.AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject user, GraphResponse response) {
if (user != null) {
doLoginFb(user);
}
}
});
request.executeAsync();
After V2.0API user id is "app-scoped" so the id can be different for same user on different apps.
You can refer to the documentation here for more details.
It also says this :
No matter what version they originally used to sign up for your app,
the ID will remain the same for people who have already logged into
your app. This change is backwards-compatible for anyone who has
logged into your app at any point in the past.
If you're not mapping IDs across apps, then no code changes should be required.
Get some public profile of user:
Bundle params = new Bundle();
params.putString("fields", "id,name,email");
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me",
params,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
Log.e("JSON",response.toString());
JSONObject data = response.getJSONObject();
//data.getString("id"),
//data.getString("name"),
//data.getString("email")
} catch (Exception e){
e.printStackTrace();
}
}
}
).executeAsync();

How to get user email and birthday from Facebook API Version v2.4

I am not able to get the facebook user email in API version v2.4. I have one app in api version2.3 it returns the email and other details of the user but now facebook updated the api version to new application. if i use the old application like veriosn <=2.3 its returning the user email by using the following code. But in api version 2.4 i am not able to get the email and date of birth.
GraphRequest.newMeRequest(result.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
{
#Override
public void onCompleted(JSONObject me, GraphResponse response)
{
if (response.getError() != null)
{
// handle error
}
else
{
Log.e("",""+me.toString());
Log.e("",""+response.getJSONObject().toString());
}
}
}).executeAsync();
You'll have to explicitly define which fields you want to retrieve with your request, for example /me?fields=id,name,email,birthday instead of just /me
See
https://developers.facebook.com/docs/apps/changelog#v2_4
To try to improve performance on mobile networks, Nodes and Edges in v2.4 requires that you explicitly request the field(s) you need for your GET requests. For example, GET /v2.4/me/feed no longer includes likes and comments by default, but GET /v2.4/me/feed?fields=comments,likes will return the data. For more details see the docs on how to request specific fields.
Since v2.4 you need include in your request the fields that you need.
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject userMe, GraphResponse response) {
if(userMe!=null){
//...... do your things
}
}
});
Bundle parameters = new Bundle();
//Add the fields that you need, you dont forget add the right permission
parameters.putString("fields","email,id,name,picture,birthday");
request.setParameters(parameters);
//Now you can execute
GraphRequest.executeBatchAsync(request);
Here is code block that worked for me:
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
final JSONObject jsonObject = response.getJSONObject();
try{
email = jsonObject.getString("email");
first_name = jsonObject.getString("first_name");
last_name = jsonObject.getString("last_name");
gender = jsonObject.getString("gender");
birthday = jsonObject.getString("birthday");
} catch (JSONException e){
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "email,first_name,last_name,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
Hope this helps!

Categories

Resources