开发者

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开发者_如何学JAVA! :-)


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


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=publish_stream


As the link says: enter link description here

<?php 
  $app_id = "YOUR_APP_ID";
  $app_secret = "YOUR_APP_SECRET";
  $my_url = "YOUR_POST_LOGIN_URL"; 
  $code = $_REQUEST["code"];
  if(empty($code)) {
    $dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
    . $app_id . "&amp;redirect_uri=" . urlencode($my_url) . "&amp;scope=email";
    echo("<script>top.location.href='" . $dialog_url . "'</script>");
  }
  $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
    . $app_id . "&amp;redirect_uri=" . urlencode($my_url)
    . "&amp;client_secret=" . $app_secret
    . "&amp;code=" . $code;
  $access_token = file_get_contents($token_url);
  $graph_url="https://graph.facebook.com/me/permissions?".$access_token;
  echo "graph_url=" . $graph_url . "<br />";
  $user_permissions = json_decode(file_get_contents($graph_url));
  print_r($user_permissions);
?>


To clarify, 'post' here refers to the HTTP method, as in GET/POST. See https://github.com/facebook/php-sdk/blob/master/src/base_facebook.php : protected function _graph($path, $method = 'GET', $params = array())

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


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 the response:

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


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


Instead of using the below code

[facebook dialog:@"feed"
 andParams:params 
 andDelegate:self]; 

Use the following solution

[facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜