views:

1676

answers:

4

Hi,

I want to display my public fanpage feed onto my website via the Facebook API without requiring a login.

I'm doing this

require_once('../includes/classes/facebook-platform/php/facebook.php');

$fb = new Facebook($api_key, $secret);
$fb->api_client->stream_get('',$app_id,'0','0','','','','',''));

But I get this error

 Fatal error:  Uncaught exception 'FacebookRestClientException' with message 'user id parameter or session key required' in includes/classes/facebook-platform/php/facebookapi_php5_restlib.php:3065
 Stack trace:
#0 includes/classes/facebook-platform/php/facebookapi_php5_restlib.php(1915): FacebookRestClient->call_method('facebook.stream...', Array)
#1 facebook/api.php(12): FacebookRestClient->stream_get('', 13156929019, '0', '0', 30, '', '', '', '')
#2 {main}
  thrown in includes/classes/facebook-platform/php/facebookapi_php5_restlib.php on line 3065

Then I figured, because of 'user id parameter or session key required', to add my user id to the call

require_once('../includes/classes/facebook-platform/php/facebook.php');

$fb = new Facebook($api_key, $secret);
$fb->api_client->stream_get(502945616,13156929019,$app_id,'0','0','','','','',''));

But then I got this error

Fatal error:  Uncaught exception 'FacebookRestClientException' with message 'Session key invalid or no longer valid'

I'm totally clueless :)

+2  A: 

Here's what i did

1) Login to facebook.

2) Grant your application offline access to your account: http://m.facebook.com/authorize.php?api_key=YOUR_API_KEY&v=1.0&ext_perm=offline_access

3) Add read stream permission http://m.facebook.com/authorize.php?api_key=YOUR_API_KEY&v=1.0&ext_perm=read_stream

4) Generate a key code http://www.facebook.com/code_gen.php?v=1.0&api_key=YOUR_API_KEY

5) Run this script once and copy the "session_key".

$facebook = new Facebook($api_key, $api_secret);
$infinite_key = $facebook->api_client->auth_getSession(YOUR_KEY_CODE);
print_r($infinite_key);

6) Plug and play!

$facebook->api_client->user = YOUR_FACEBOOK_USER_ID
$facebook->api_client->session_key = YOUR_INFINITE_KEY
$facebook->api_client->expires = 0;
$feed = $facebook->api_client->stream_get(YOUR_FACEBOOK_USER_ID, FAN_PAGE_ID);

So bascially, this will grab the feed from your "perspective" but filter it only to show items from the specified Fan page.

Disclaimer: This works, but I'm not sure whether this is a "supported" method for grabbing data, or even allowed at all.

References:

http://www.emcro.com/blog/2009/01/facebook-infinite-session-keys-no-more/

http://blog.jylin.com/2009/10/01/loading-wall-posts-using-facebookstream_get/

Daniel
Awesome! Thanks a lot!
Bundy
After I generate my keycode, I get this message:Success! Your one-time code isXXXXXXTo keep yourself permanently logged in to APP_NAME , you should provide this code to the application.How do I provide this to the application? Because now, everytime I make an api call I have to regenerate this code. I tried this a couple of times and now I get this error: Uncaught exception 'FacebookRestClientException' with message 'Invalid parameter'
Bundy
After generating the keycdode (step 4) you use that code in step 5 to generate an array that has a infinite session key, this you will only have to do once. You then use the infinite session key in step 6 (where it says YOUR_INFINITE_KEY), that's how you provide it to the application, did you do this?
Daniel
Ok, so I did the steps all over again. I generated the keycode. Then I provided my user_id, the session key and set expires to 0. No errors on that. But then when I try to execute an API call eg stream_get() or an fql_query, I get this error: Fatal error: Uncaught exception 'FacebookRestClientException' with message 'Incorrect signature'Btw: I'm doing this on localhost
Bundy
Ok, I think I'm getting somewhere, I set my app type to website in stead of desktop and I'm getting some result
Bundy
A: 

ok, i tried many times, but it worked when I added QUOTES to code :-)) $infinite_key = $facebook->api_client->auth_getSession('YOUR_KEY_CODE'); THANKS for suggestion!

bubu
A: 

The step 5 generate this error 'FacebookRestClientException' with message 'Incorrect signature' why?

alexandra
A: 

I get the same error with the icorrect signature in step 5. Any idea?

Treehacker