Unable to get age_range from Facebook API

According to the documentation on Facebook, age_range is the default property when requesting user data: https://developers.facebook.com/docs/facebook-login/permissions#reference-basic-info

This works when I use " me " as the user id with the corresponding token:

https://graph.facebook.com/me?fields=id%2Cname%2Cemail%2Cfirst_name%2Clast_name%2Cusername%2Cgender%2Cpicture%2Cage_range&format=json&access_token=[accessToken for the required user]

{ "id": "FACEBOOKID", "name": "testuser", "email": "testuser\u0040test.net", "first_name": "testuser", "last_name": "testuser", "gender": "male", "age_range": { "min": 21 }, "picture": { "data": { "url": "https://...", "is_silhouette": false } } } 

But data_range is then empty when I use user id :

https://graph.facebook.com/[FacebookId.BIZ?fields=id%2Cname%2Cemail%2Cfirst_name%2Clast_name%2Cusername%2Cgender%2Cpicture%2Cage_range&format=json&access_token=โ–บAccessToken] *

Gives me the answer:

 { "id": "FACEBOOKID", "name": "testuser", "email": "testuser\u0040test.net", "first_name": "tftestuser", "last_name": "tftestuser", "gender": "male", "picture": { "data": { "url": "http://....", "is_silhouette": true } } } 

Any idea why? What am I missing here?

Thank you in advance!

+6
source share
4 answers

I found the answer using the API here: no age_range in facebook answer (javascript sdk)

The api-call "/ me" returns the default information, which does not always include age_range.

However, you can request age_range when calling api: "/ Me? Fields = age_range"

+5
source

According to Platform Updates: Operation Developer Love -

available to all users who install your application

So age_range will not be returned if the user has not installed your application. As in your case, this user may not use the application for which you receive this space.

Also, the purpose of the age_range field is to allow your application to determine whether your application can provide specific age content. Thus, the search age_range for the user's friends is inappropriate, and you need to get permission friends_birthday .

EDIT:

The link also says -

mobile apps and websites that use Facebook Login to restrict their content to people over the age of 18 and 21+

This means that this field is only available for applications that have limited their content to people aged 18+ and 21+ - check!

+4
source

I also spent several hours finding a solution for this.

Share your experience below:

First, I request a fb session, requesting user data with the following permissions:

 session.openForRead(new com.facebook.Session.OpenRequest( LoginActivity2.this).setPermissions( Arrays.asList("public_profile","email")).setCallback( statusCallback)); 

In a valid session, I then invoke the request using this code block:

 new Request( session, graphPath, bundle, HttpMethod.GET, new Request.Callback() { @Override public void onCompleted(Response response) { Log.v(TAG, "response = xx" + response.toString()); String age_min = ""; String age_max = ""; GraphObject graphObject = response.getGraphObject(); Map<String, Object> stateMap = graphObject.asMap(); try { JSONObject age_range = (JSONObject) stateMap.get("age_range"); age_min = age_range.getString("min"); age_max = age_range.getString("max"); } catch (Exception e) { Log.v(TAG, "error in parsing age_range here = " + e.getMessage()); } Log.v(TAG, "logging the age_range min here = " + age_min); Log.v(TAG, "logging the age_range max here = " + age_max); if (!"".equalsIgnoreCase(age_max)) { if ("18".equalsIgnoreCase(age_max)) { age_range = "0-18"; } else if ("21".equalsIgnoreCase(age_max)) { age_range = "18-21"; } } else if (!"".equalsIgnoreCase(age_min)) { if ("18".equalsIgnoreCase(age_min)) { age_range = "18-21"; } else if ("21".equalsIgnoreCase(age_min)) { age_range = "21-100"; } } Log.v(TAG, "and here in FB2 the age_range is = " + age_range); } } ).executeAndWait(); 

Where:

session (string) -> current and current session

graphPath (string) -> this is fb userID (maybe "I", but I have not tested this, please let me know if "I" works.

bundle โ†’ is the K, V parameter that you request, in case of age_range this is:

 Bundle bundle = new Bundle(); bundle.putString("fields", "age_range"); 

Note that the request code block (at least in my case) was executed inside the doinBackground of the internal AsyncTask) -> thus, my use of the executeAndWait () method instead of executeAsync ().

Hope this helps someone with the same issue. Thanks!

+1
source

You can only get the current registered user OWN age_range and cannot request a different age, so you will not receive a response if the provided user_id does not match your own id

see fooobar.com/questions/966126 / ...

You need to explicitly tell the API that you need age_range as part of a public profile (no special permissions are required). The age of the user can be 13-17, 18-20 or 21+.

 /* make the API call v.2.3 */ FB.api( "/me?fields=age_range", function (response) { if (response && !response.error) { /* handle the result */ } } ); 

read the documentation https://developers.facebook.com/docs/graph-api/reference/age-range/

+1
source

All Articles