Facebook PHP Graph API not returning complete data

2019-07-29 15:56发布

问题:

I'm having an issue with Facebook's PHP SDK version 4.0.

I'm simply trying to retrieve the insights/page_fans_city metric via the Graph API:

$request = new FacebookRequest($session, 'GET', 
    '/{my object}/insights/page_fans_city', 
    array('period' => 'lifetime'));

$response = $request->execute();
$client_graph_object = $response->getGraphObject();

print_r($client_graph_object);


However, no data is returned:

Facebook\GraphObject Object
(
[backingData:protected] => Array
    (
        [data] => Array
            (
            )

        [paging] => stdClass Object
            (
                [previous] => https://graph.facebook.com/v2.0/...
                [next] => https://graph.facebook.com/v2.0/...
            )

    )

)


I know that I have the Insights data that I'm requesting. I can make the exact same request via the Graph Explorer:

→ /v.2.0/{my object}/insights/page_fans_city?period=lifetime

{
  "data": [
    {
      "id": "{my object}/insights/page_fans_city/lifetime", 
      "name": "page_fans_city", 
      "period": "lifetime", 
      "values": [
        {
          "value": {
            ...
          },
          "end_time": "2014-08-01T07:00:00+0000"
        },
        {
          "value": {
            ...
          },
          "end_time": "2014-08-02T07:00:00+0000"
        }
      ], 
      "title": "Lifetime Likes by City", 
      "description": "Lifetime: Aggregated Facebook location data, sorted by city, about the people who like your Page. (Unique Users)"
    }
  ], 
  "paging": {
    "previous": "https://graph.facebook.com/v2.0/..., 
    "next": "https://graph.facebook.com/v2.0/...
  }
}


I should note that I am receiving data if I omit the page_fans_city segment. When it's omitted- I can view data for page_fans_country only. Therefore

$request = new FacebookRequest($session, 'GET', 
    '/{myobject}/insights/page_fans_country', 
    array('period' => 'lifetime'));

and

$request = new FacebookRequest($session, 'GET', 
    '/{myobject}/insights', 
    array('period' => 'lifetime'));

will work for me ...

I suspect it's my Access token- but I'm not sure why that would be the case. The app that I created only requires the read_insights permission.

Any thoughts or suggestions?


Thanks in advance,
Mike

回答1:

It was a permissions issue. If you ever see:

{
    "data": [
    ],
    ...
}

It's likely your access token does not have sufficient privileges to access the data you're requesting (that or there's simply no data).

My problem was that even though I was the Developer of the app and it was approved by Facebook (with the manage_pages and read_insights permissions)- I could not use an app access token to retrieve the insights data.

I needed a user access token!

I easily generated the user access token by manually creating a login flow per Facebook's documentation. This stackoverflow thread was useful, too.

More info on Facebook tokens here.

Hope this helps anyone who stumbles across this thread!

-Mike