如何使用FB Graph在动态消息(墙)上发布一条信息

12
我已经创建了一个应用程序,现在我想通过使用新的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..')
);

这段代码没有报错,而且我知道我的访问令牌是正确的(否则我就无法运行 $facebook->api('/me?access_token='.$this->access_token); 来获取我的用户对象)。

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

7个回答

18

好的,我最终解决了这个问题。感谢 phpfour 的帮助 :-)

首先: 我的连接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..')
);

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

今天我正在使用Facebook::getLoginUrl()进行授权。在"req_perms"下添加了"publish_stream",但是我猜这还不够好:( - qualbeen
如果你卡住了,可以看看我的最新博客文章,我在里面展示了一个可行的例子:http://bit.ly/cT40vV。 - Mohammad Emran Hasan
1
值得注意的是,应该是“publish_stream”,而不是“stream_publish”。请参阅http://developers.facebook.com/docs/authentication/。 - Nicholas Head
1
你的教程已经不存在了,Google缓存似乎找不到它 - 你有新的链接可用吗? - Mark Mayo

1

除了瑞士法郎之外,

发布后:

$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或者code

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

1
如链接所示:在此输入链接描述
<?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);
?>

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->api( '/me/feed/', 'post', array('access_token' => $this->access_token, 'message' => 'Playing around with FB Graph..') );


0

这是获取访问权限的旧方法。在 GRAPH 中,我首先生成了代码密钥:

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

现在我们拥有code密钥后,可以从以下链接生成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会将您的消息发布为用户而不是应用程序...为什么?!

如果您想在应用程序墙上发布,请使用:

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

0

不要使用下面的代码

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

请使用以下解决方案。
[facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接