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

发布于 2024-08-31 23:20:30 字数 1080 浏览 5 评论 0原文

我已经创建了一个应用程序,现在我想使用新的 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 成功发布了消息吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

忆离笙 2024-09-07 23:20:30

好吧,我终于解决了这个问题。感谢 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..')
);

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..')
);
随遇而安 2024-09-07 23:20:30

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

为了获得扩展权限,请通过以下方式获取授权令牌:

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

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
放赐 2024-09-07 23:20:30

正如链接所示: 输入链接此处描述

<?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 . "&redirect_uri=" . urlencode($my_url) . "&scope=email";
    echo("<script>top.location.href='" . $dialog_url . "'</script>");
  }
  $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url)
    . "&client_secret=" . $app_secret
    . "&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);
?>

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 . "&redirect_uri=" . urlencode($my_url) . "&scope=email";
    echo("<script>top.location.href='" . $dialog_url . "'</script>");
  }
  $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url)
    . "&client_secret=" . $app_secret
    . "&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);
?>
云朵有点甜 2024-09-07 23:20:30

澄清一下,这里的“post”是指 HTTP 方法,如 GET/POST 中。
请参阅 https://github.com/facebook/php-sdk/ blob/master/src/base_facebook.php
受保护函数_graph($path, $method = 'GET', $params = array())

$结果 = $facebook->api(
'/我/饲料/',
'邮政',
array('access_token' => $this->access_token, 'message' => '玩转 FB Graph..')
);

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

无声情话 2024-09-07 23:20:30

除了 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 或 code

$facebook->api( '/YOUR_APPID/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..'));
柳若烟 2024-09-07 23:20:30

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

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

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..'));
白首有我共你 2024-09-07 23:20:30

而不是使用下面的代码

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

使用以下解决方案

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文