6

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

This does work when I use "me" as the user-id with the proper 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 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 the data_range is then empty when I use the user id:

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

Gives me back:

{
   "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!

Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
snessarb
  • 85
  • 1
  • 1
  • 6

4 Answers4

5

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

The api call "/me" returns the default info, which doesn't always include age_range.

However you can request age_range with the api call: "/me?fields=age_range"

Community
  • 1
  • 1
user3203348
  • 312
  • 3
  • 7
4

According to Platform Updates: Operation Developer Love -

accessible for all users that install your app

So age_range won't be returned if the user hasn't installed your app. As in your case, that user might not be using the app for which you are getting this blank.

Also the purpose of age_range field is to let your app determine whether your app can provide some age-sensitive contents. So, retrieving age_range for user's friends is inappropriate and you'll have to get friends_birthday permission.

EDIT:

The link also says-

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

That means this field is available only for the apps who have restricted their content to people age 18+ and 21+- please check!

Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
  • Thank you for your response. In my case, the user I use is a test user created via the Facebook app settings, so the user has my app installed by default (I checked the corresponding checkbox) ... – snessarb Mar 20 '14 at 13:55
  • Sorry, I pressed the enter key to fast so I was not done writing my comment :/ my issue is not yet solved ... – snessarb Mar 20 '14 at 13:57
  • Have you set the age restriction to 18+/21+ in the App settings? – Sahil Mittal Mar 20 '14 at 14:11
  • 2
    Good point. I tried to update my app so that it is for 18+ but this does not change the result. I still don´t get the age_range ... my test user is 21 I think ... – snessarb Mar 20 '14 at 15:56
  • Strange. None of my apps have such age restriction, so if you are sure you can report this bug here: https://developers.facebook.com/x/bugs/trending/ – Sahil Mittal Mar 20 '14 at 17:41
  • 3
    This answer is incorrect. Even after a user gives the application permission (aka installing the app), age_range is still missing. – Metablocks Corp Jun 23 '14 at 20:01
1

I too spent hours on trying to find a solution for this.

Am sharing my experience below:

I initially asks for an fb session by asking for user details with the following permissions:

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

With a valid session I then call a 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) --> valid and currently opened session

graphPath (string) --> this is the fb userID (can be "me" but I haven't tested that, please let me know if "me" works.

bundle --> is the K,V parameter that you will request, in the age_range case it is:

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

Please note that the request code block (at least in my case) was done inside the doinBackground of inner AsyncTask) --> thus my use of the executeAndWait() method instead of the executeAsync().

Hope this will help someone with the same problem. Thanks!

Marka A
  • 258
  • 5
  • 15
  • Facebook Session is removed https://stackoverflow.com/questions/32232606/android-facebook-integration-impossible-to-import-com-facebook-session – Harvey May 24 '18 at 11:16
1

You can retrieve only currently logged-in user's OWN age_range and can not query other's age therefore you get no response when provided user_id is not the same as own id

see https://stackoverflow.com/a/30731037/4990217

You'll have to explicitly tell the API that you need age_range, as part of the public profile (no special permissions required). The user's age range may 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 documentation https://developers.facebook.com/docs/graph-api/reference/age-range/

Community
  • 1
  • 1
Buzz
  • 370
  • 2
  • 6
  • hm i use https://graph.facebook.com/me?fields=id,first_name,last_name,location,hometown,address,birthday,age_range&access_token= and i get "age_range": { "min": 21 }, there is no max – Toolkit Nov 12 '16 at 09:45
  • @Toolkit where you able to find how to to get the max value, i'm also just getting a min value from facebook – jasan Dec 05 '16 at 00:17
  • @jasan seems like 21 is all there is, i.e. it is the maximum, I saw it in official docs somewhere – Toolkit Dec 05 '16 at 10:25