Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can´t get age_range from Facebook API

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!

like image 935
snessarb Avatar asked Mar 20 '14 12:03

snessarb


3 Answers

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"

like image 116
user3203348 Avatar answered Nov 18 '22 18:11

user3203348


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!

like image 4
Sahil Mittal Avatar answered Nov 18 '22 17:11

Sahil Mittal


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!

like image 1
Marka A Avatar answered Nov 18 '22 17:11

Marka A