12

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

我已经在使用 oAuth 和 Graph-api 来获取我所有朋友的列表。http://developers.facebook.com/docs/api上的 API告诉我 cURL https://graph.facebook.com/[userid]/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 发布了一条消息?那我需要你的帮助!:-)

4

7 回答 7

18

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

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

第二;我试图通过

$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..')
);
于 2010-05-12T12:40:06.573 回答
6

您需要“publish_stream”扩展权限才能写入提要。以下是它们的完整列表: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
于 2010-05-06T19:57:59.090 回答
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);
?>
于 2011-05-06T12:06:42.953 回答
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..'));
于 2010-08-23T05:31:11.780 回答
1

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

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

于 2011-08-03T16:43:09.380 回答
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 而不是 APPLICATION... 为什么?!

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

$facebook->api( '/YOUR_APPID/feed/', 'post', array('access_token' => $this->access_token, 'message' => 'Playing around with FB Graph..'));
于 2010-06-19T01:15:13.390 回答
0

而不是使用下面的代码

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

使用以下解决方案

[facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];
于 2012-11-28T06:52:32.950 回答