首页 文章

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

提问于
浏览
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..')
);

这段代码不会抛出任何错误,我知道我的access_token是正确的(否则我无法运行$ facebook-> api('/ me?access_token =' . $ this-> access_token);来获取我的userobject .

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

7 回答

  • 0

    好的,我终于解决了这个问题 . Thanx到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)
    );
    

    Second ;我试图通过发布到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

    您需要"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
    
  • 18

    如链接所示: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 . "
    "; $user_permissions = json_decode(file_get_contents($graph_url)); print_r($user_permissions); ?>
  • 0

    为了澄清,'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'=>'玩FB Graph ..') );

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

    这是获取访问权限的旧方法 . 在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

    而不是使用下面的代码

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

    使用以下解决方案

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

相关问题