Posting to facebook event wall

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

Posting to facebook event wall

Post by invisibled »

Hey Everybody,

I've been working on a script to post to various social networks. I'm a fairly strong programmer, but have never worked with OAuth or the Facebook graph api. So as the title says, i'm trying to write a script that will make a post onto a facebook event wall.

Here is the code i have so far

Code: Select all

require 'facebook-php-sdk/src/facebook.php';

    $fb = new Facebook(array(
      'appId'  => '100164043386901',
      'secret' => '74068d49e68012612e2440eea74f0301',
      'cookie' => true,
    ));
	

	$fb->api('/me/feed', 'POST', array(
	    'message' => 'This is a test post',
	    'picture' => '',
	    'link' => '',
	    'name' => '',
	    'caption' => '',
	    'description' => '',
	    'icon' => ''
	));
and this is to post to my feed... just as a test. As of right now, it says i don't have permissions. One thing to note is that this script will be executed by a cronjob, so i need to enable access for as long as possible and used the following url to get the access token. (you will notice in my scope i have offline_access and publish_stream).

https://graph.facebook.com/oauth/author ... client_id=[my id]&redirect_uri=[my uri]&scope=offline_access,publish_stream

So that is all good, i accept the permissions and it redirects back to my script with the access token in the url. And now i'm stuck. What do i do with the access token? How do i finish the authentication process?

Thank you in advance for any replies, i will be checking this all day to get as fast as a resolution as possible. If you need any info just ask!
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Posting to facebook event wall

Post by Eric! »

I haven't done this before on facebook, but I've been thinking about linking my blog with it.

I think the process is:
1. Setup your app in http://www.facebook.com/developers/ to get your API KEY for your application
2. Get a one time session key to setup your permanent key viahttps://login.facebook.com/code_gen.php ... _KEY&v=1.0 where API_KEY is from step 1
3. Get permanent session key with something like this

Code: Select all

<?php
// FB_APIKEY is your facebook application api key
// FB_SECRET is your application secrete key
$FB_APIKEY="YOUR_API";
$FB_SECRET="YOUR_SECRET";
$fb = new FacebookRestClient($FB_APIKEY, $FB_SECRET);
$testtoken= "ONETIMETOKEN"; // Replace this value with your Token Value
$result = $fb->call_method('facebook.auth.getSession',
array('auth_token' => $testtoken, 'generate_session_secret' => true));
echo "<br /><pre>";
print_r($result);
echo $session_key = $result['session_key'];
?>
4. Set your wall permissions for your API_KEY with this link (put your key in)
http://www.facebook.com/login.php?api_k ... ine_access
You should be given 3 options, if they don't appear then your call back URL or Canvas URL must be incorrect, please check your application settings and execute the URL again.
You should get allowpermission, allowpublishing and then on final page, you will see a success message. That’s it you have given permission for read_stream,publish_stream and offline_access.
5. You should then be able to publish now and setup your cronjob with something like this:

Code: Select all

<?php
define('FB_APIKEY', 'YOUR_APIKEY');
define('FB_SECRET', 'YOUR_SECRET');
define('FB_SESSION', 'YOUR_SESSION_key');
require_once('facebook-platform/php/facebook.php');
echo "post on wall";
try {
$facebook = new Facebook(FB_APIKEY, FB_SECRET);
$facebook->api_client->session_key = FB_SESSION;
$fetch = array('friends' =>
array('pattern' => '.*',
'query' => "select uid2 from friend where uid1={$user}"));
echo $facebook->api_client->admin_setAppProperties(array('preload_fql' => json_encode($fetch)));
$message = 'From My App: publish steven on facebook';
if( $facebook->api_client->stream_publish($message))
echo "Added on FB Wall";
} catch(Exception $e) {
echo $e . "<br />";
}
?>
It seems like maybe your session key is missing or maybe the api_key permissions are set properly?
Post Reply