7

I've been trying for two days to post messages gathered from a twitter search to one of my facebook pages automatically - i.e. via a cronjob.

The twitter part went fine, but for the life of me i can't get the Facebook part to work.

The problem is that my script works... until it doesn't, usually the access_token is expired after a few hours.

Now i have this message : #200) Posts where the actor is a page cannot also include a target_id.

I've tried many things suggested on various SO threads. Problem is: the Facebook API seems to change quite often and what used to work doesn't.

Any idea and suggestion as to how to make it work reliably is welcome.

Here is the code I have so far. I've created a facebook app, and generated an access token using the FB Graph Explorer and a request to '/me/account'.

require('config.inc.php');
require('_classes/facebook-php-sdk/src/facebook.php');

// Connect to facebook
$facebook = new Facebook(array(
        'appId'  => FB_APP_ID,
        'secret' => FB_APP_SECRET,
    ));

// get the message
$msg_body = array(
    'message' => $message->message."\n".'(via http://twitter.com/'.$message->author.')',
    'access_token' => FB_ACCESS_TOKEN 

);
// Post to Facebook
$fb_result=0;
try {
    $postResult = $facebook->api('/'.PAGEID.'/feed', 'post', $msg_body );
} catch (FacebookApiException $e) {
    echo $e->getMessage();
}

if($postResult)
{
    $fb_result=1;
    $last_posted_tweet_id = $message->id;
    file_put_contents(FOLDER.LAST_TWEET_ID_FILE, $last_posted_tweet_id);
    echo 'Your message '.$message->id.' is posted on your facebook wall.';
    //print_r($msg_body);
}

UPDATE Code is visible here http://phpbin.net/ZMNt3MPt

pixeline
  • 17,669
  • 12
  • 84
  • 109

2 Answers2

10

I was having a similar problem with the access token expiring. Turns out you can exchange your token to "long lived" token

Managed to dig up my code:

try{
        $token =  $facebook->getAccessToken();

        // get "long-lived" access token
        $curl = new Curl();
        $curl->setSsl();
        $exchange_url = "https://graph.facebook.com/oauth/access_token?client_id=".$facebook_app_id."&client_secret=".$facebook_app_secret."&grant_type=fb_exchange_token&fb_exchange_token=".$token;
        $page = $curl->get($exchange_url);

        if ($page){
            $page = explode("access_token=", $page);
            if (count($page) > 1){
                $page = explode("&", $page[1]);
                $token = $page[0];

                $facebook->setAccessToken($token);
            }
        }

    } catch(Exception $e){
        $token = '';
    }
lePunk
  • 523
  • 3
  • 10
  • thank you. How do i use the token from then on? I tried using $token with the message body token key, but then i get `The user hasn't authorized the application to perform this action` . Lost, i am. I've posted my entire code here, so you have an overview. http://phpbin.net/fTVJgkrF – pixeline Jul 22 '13 at 19:03
  • 1
    I suspect you're waiting for the token to expire and *then* exchanging it. Do the exchange as soon as you get a token in the first place. See "Getting Info about Tokens and Debugging" at https://developers.facebook.com/docs/facebook-login/access-tokens/ – Reuben Thompson Jul 26 '13 at 14:43
  • Hello +lePunk: did you have a chance to look at the code i posted? I'm a bit desperate for a working answer at this point. – pixeline Jul 31 '13 at 16:28
  • before you can use the code above you have to obtain a normal access token from the user. you can do that by using the facebook connect button. right after the user connected you do the exchange to the long lived access token with the code above and you save the token in your db. then in your cronjob you just call the setAccessToken method with the token from the db and it should work. also it might worth doing the exchange in the cronjob too, so that way you always have a fresh token – lePunk Jul 31 '13 at 16:42
1

They are other alternative you can use, for me i find it easier to use Twitter Api to post comment on twiiter and facebook at the same time. I linked it to Facebook, it works great all you will just have to do is change the twitter api key and supply data. If you are interested in this solution let me know and i will post the code here

  • I understand, but i really need to have it in that direction: from twitter to facebook. And not the other way around. – pixeline Jul 31 '13 at 16:25
  • My solution works the same way. It post to Twitter first and because my twitter is linked to my Facebook, any post on twitter is then posted on Facebook automatically – Mac Chibueze Aug 01 '13 at 08:27
  • question is: how did you link your twitter to your facebook? – pixeline Aug 01 '13 at 19:10
  • visit [www.twitter.com] goto settings->profile scroll down to the bottom of the page you should see connect(link) your twitter account to Facebook. – Mac Chibueze Aug 02 '13 at 10:39
  • It doesn't work for me. Besides, i want to publish tweets with a specific hashtags, not only authored from my account. Thank you for the suggestion, though. – pixeline Aug 08 '13 at 16:41