Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto use FB Graph to post a message on a feed (wall)

I have created an app, and now i want to post a message on one of my friends wall with use of the new Graph API. Is this do-able?

I am already using oAuth and the Graph-api to get a list of all my friends. The API at http://developers.facebook.com/docs/api tells me to cURL https://graph.facebook.com/[userid]/feed to read the feed, but it also tells me howto post a message:

curl -F 'access_token=[...]' -F 'message=Hello, Arjun. I like this new API.' https://graph.facebook.com/arjun/feed

Ofcourse this doesn't work! And I can't find out why..

Here are my PHP-code:

require_once 'facebook.php'; // PHP-SDK downloaded from http://github.com/facebook/php-sdk
$facebook = new Facebook(array(appId=>123, secret=>'secret'));
$result = $facebook->api(
        '/me/feed/',
        array('access_token' => $this->access_token, 'message' => 'Playing around with FB Graph..')
);

This code does not throws any error, and I know my access_token are correct (otherwise i could't run $facebook->api('/me?access_token='.$this->access_token); to get my userobject.

Have anyone out there sucsessfully posted a message using Graph-api? Then i need your help! :-)

like image 410
qualbeen Avatar asked May 06 '10 18:05

qualbeen


People also ask

What is the use of Facebook graph?

The Facebook Graph API is an HTTP-based API that allows developers to extract data and functionality from the Facebook platform. Applications can use this API to programmatically query data, post in pages and groups, and manage ads, among other tasks. The other Facebook APIs are extensions of the Graph API.

How do I make a graph API on Facebook?

Go to Tools – Graph API Explorer to generate Access Tokens for various kinds of API that you would like to use in your app. Choose the app and kind of token you need in the drop down menus in the left part of the screen. Then click “Generate Access Token”.


1 Answers

Okay, I finally solved this. Thanx to phpfour for your help :-)

First: My connection-url looks like this (with "publish_stream"):

$connectUrl = $this->getUrl(
  'www',
  'login.php',
  array_merge(array(
    'api_key'         => $this->getAppId(),
    'cancel_url'      => $this->getCurrentUrl(),
    'req_perms'       => 'publish_stream',
    'display'         => 'page',
    'fbconnect'       => 1,
    'next'            => $this->getCurrentUrl(),
    'return_session'  => 1,
    'session_version' => 3,
    'v'               => '1.0',
  ), $params)
);

Second; I tried to post to facebook via

$result = $facebook->api(
    '/me/feed/',
    array('access_token' => $this->access_token, 'message' => 'Playing around with FB Graph..')
);

But the correct way to do this is include one more parameter ('post'):

$result = $facebook->api(
    '/me/feed/',
    'post',
    array('access_token' => $this->access_token, 'message' => 'Playing around with FB Graph..')
);
like image 173
qualbeen Avatar answered Sep 28 '22 03:09

qualbeen