5

For example, here's a 'page':

http://www.facebook.com/facebook

That page has an RSS feed (which I'd like to use, ideally), but a) it browser-sniffs meaning I need to fake the user-agent from a script to fetch it - and that feels really brittle b) the quality of the data returned is really poor.

Can I use the graph api to fetch the same data? This URL:

https://graph.facebook.com/facebook/feed

implies that I can, and json is fine for me, although I'm fetching this from a PHP script rather than client-side. However, when I try that URL for my actual page, I get the following:

{
    "error": {
        "type": "OAuthAccessTokenException",
        "message": "An access token is required to request this resource."
    }
}

I don't understand why an access token is required for my page, whilst other pages are 'more public' - is that a configuration on the page somewhere? If not, what's the best way of obtaining the access key - note that this is not an interactive script asking the page owner to authenticate.

Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
Bobby Jack
  • 15,689
  • 15
  • 65
  • 97
  • Have you tried to connect with your user before fetching your URL? – Doomsday Jul 20 '10 at 09:42
  • This is where terminology starts causing problems... It's a 'page' I'm talking about here, but when you say "your user", do you mean a user who's an admin of the page? I'm still trying to get my head around the (enormous and complex) facebook data model. – Bobby Jack Jul 20 '10 at 09:51

2 Answers2

5

If I try to access the URL via CURL, it works OK for me in PHP.

$curlResponse = http('https://graph.facebook.com/facebook/feed');
$facebookFeed = json_decode($curlResponse['data'], true);

var_dump($facebookFeed);

Using this php function:

function http($url) {
  $timeout = 30;
  $connectTimeout = 30;
  $sslVerifyPeer = false;

  $response = array();
  $ci       = curl_init();

  /* Curl settings */
  curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $connectTimeout);
  curl_setopt($ci, CURLOPT_TIMEOUT, $timeout);
  curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
  curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $sslVerifyPeer);    
  curl_setopt($ci, CURLOPT_URL, $url);

  $response['http_code'] = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  $response['api_call']  = $url;
  $response['data']      = curl_exec($ci);

  curl_close ($ci);

  return $response;
}
eillarra
  • 5,027
  • 1
  • 26
  • 32
  • So it seems that the data is public and no authentification is needed. – eillarra Jul 22 '10 at 13:57
  • Well, some data seems to be public https://graph.facebook.com/104783309570620/feed (this is a website page) https://graph.facebook.com/141228739224419/feed (a store) and some no... https://graph.facebook.com/104412186277028/feed (a music band page) – eillarra Jul 22 '10 at 14:07
  • Strange - the corresponding URL for our 'page' was NOT behaving in that manner, but IS now! Either facebook have made a change, or someone has changed our 'page' in some way to allow this. Your answer is correct, even though it was what I was trying anyway :-) so have some bounty! – Bobby Jack Jul 26 '10 at 09:34
2

Update: As of June 3, 2011, a valid app or user access_token is required with all API requests.

For more info: http://developers.facebook.com/blog/post/509/

Ray
  • 150
  • 1
  • 7