views:

4967

answers:

6

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! :-)

+2  A: 

Hi There,

You'll need the "publish_stream" extended permission in order to write to the feed. Here is a complete list of them: http://developers.facebook.com/docs/authentication/permissions.

In order to get the extended permission, get the authorization token in this way:

https://graph.facebook.com/oauth/authorize?
client_id=...&
redirect_uri=http://www.example.com/callback&
scope=stream_publish
phpfour
Today i'm using Facebook::getLoginUrl() to authorize. I've added "publish_stream" under "req_perms", but I guess this isn't good enough :(
qualbeen
Well, if you are stuck you can have a look at my recent blog post where I have shown a working example: http://bit.ly/cT40vV
phpfour
It's worth noting that it should be "publish_stream", not "stream_publish". See http://developers.facebook.com/docs/authentication/
Nicholas H
+3  A: 

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..')
);
qualbeen
A: 

Hi, I new in API Facebook, this is great...

$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) );

But...

When I want to post to my wall facebook:

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

Fatal error: Using $this when not in object context in /home/XXX/public_html/FACEBOOK_DIR/index on line "N"

The error is $this->access_token

Any can help me??

Regards,,

Pako
A: 

That is old way to get acces. In GRAPH first i generated code key with:

$getLinkCode ='https://graph.facebook.com/oauth/authorize'.
              '?client_id=YOUR_APPID'.
              '&redirect_uri=YOUR_SITE'.
              '&scope=publish_stream';

And now when we have code key we can generate *access_token* from link:

$getLinkToken='https://graph.facebook.com/oauth/access_token'.
              '?client_id=YOUR_APPID'.
              '&redirect_uri=YOUR_SITE'.
              '&client_secret=YOUR_SECRET'.
              '&code=CODE_KEY';

But this access_token post your message as USER not APPLICATION... WHY?!

If you want post on application wall use:

$facebook->api( '/YOUR_APPID/feed/', 'post', array('access_token' => $this->access_token, 'message' => 'Playing around with FB Graph..'));
chf
A: 

in addition to chf,

after posting $getLinkToken='https://graph.facebook.com/oauth/access_token'. '?client_id=YOUR_APPID'. '&redirect_uri=YOUR_SITE'. '&client_secret=YOUR_SECRET'. '&code=CODE_KEY';

i got response like

https://graph.facebook.com/oauth/access_token? client_id=xxxxxxxxxxxxxx &redirect_uri=myurl &client_secret=xxxxxxxxxxxxxx &code=xxxxxxxxxxxxxx

no which one is access_token, client_secret or code

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

Saboor
A: 

Please is there another way to post a status to facebook via GRAPH API without using curl. my hosting provider doesn't support curl?

Tola