如何使用FB Graph在Feed(墙)上发布消息

时间:2010-05-06 18:45:50

标签: php facebook facebook-graph-api

我创建了一个应用程序,现在我想使用新的Graph API在我的一个朋友墙上发布消息。这可以吗?

我已经在使用oAuth和Graph-api来获取所有朋友的列表。 http://developers.facebook.com/docs/api上的API告诉我cURL https://graph.facebook.com/[userid]/feed阅读Feed,但它也告诉我如何发布消息:

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

当然这不起作用!我无法找出原因..

这是我的PHP代码:

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

此代码不会引发任何错误,我知道我的access_token是正确的(否则我无法运行$ facebook-> api('/ me?access_token ='。$ this-> access_token);获取我的用户工具。

有人在那里使用Graph-api成功发布消息吗?然后我需要你的帮助! :-)

7 个答案:

答案 0 :(得分:18)

好的,我终于解决了这个问题。 Thanx to phpfour求助: - )

首先:我的connection-url看起来像这样(带有“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)
);

<强>第二;我试图通过

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

但这样做的正确方法是再加一个参数('post'):

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

答案 1 :(得分:6)

您需要“publish_stream”扩展权限才能写入Feed。以下是完整列表:http://developers.facebook.com/docs/authentication/permissions

要获得扩展权限,请以这种方式获取授权令牌:

https://graph.facebook.com/oauth/authorize?
client_id=...&
redirect_uri=http://www.example.com/callback&
scope=publish_stream

答案 2 :(得分:1)

除了 chf

发布后:

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

我收到了回复:

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

没有哪个是access_tokenclient_secret或代码

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

答案 3 :(得分:1)

如链接所示: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);
?>

答案 4 :(得分:1)

为了澄清,'post'在这里指的是HTTP方法,如在GET / POST中。 见https://github.com/facebook/php-sdk/blob/master/src/base_facebook.php: protected function _graph($ path,$ method ='GET',$ params = array())

  

$ result = $ facebook-&gt; api(      '/我/饲料/',      “后”,      array('access_token'=&gt; $ this-&gt; access_token,'message'=&gt;'玩FB Graph ..')   );

答案 5 :(得分:0)

这是获取访问权限的旧方法。在GRAPH中,我首先使用:

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

现在,当我们有代码键时,我们可以从链接生成 access_token

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

但是这个access_token会将您的消息发布为USER not APPLICATION ...为什么?!

如果您想在申请墙上发帖使用:

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

答案 6 :(得分:0)

而不是使用以下代码

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

使用以下解决方案

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