以用户身份发布到FB页面

时间:2012-03-07 17:30:55

标签: php facebook-graph-api post feed facebook-wall

所以我有一个Facebook页面(我们称之为X),其上有应用程序Y.用户可以通过Y提出问题,并以用户(而不是应用程序)的身份发布到X.

我对该应用的权限目前设置为publish_stream。

我可以通过

获取令牌
    $token_url = "https://graph.facebook.com/oauth/access_token?" .
       "client_id=" . $this -> data["environment"] -> fb_appid .
       "&client_secret=" . $this -> data["environment"] -> fb_appsecret .
       "&grant_type=client_credentials";
    $app_token = file_get_contents($token_url);

给了我一个令牌就好了。

现在,如果我尝试通过APi呼叫进行POST,我会得到两个结果:

当我没有传递令牌而只是打电话

$post_id = $this ->Facebook->fb_api("/PAGE_ID/feed", "POST", array("message"=>"This is a post from PHP."));

我以JSON

的形式收到回复
{
   "id": "PAGEID_someOtherID"
}

但我看不到墙上的帖子。

当我传递访问令牌时,ala

$post_id = $this ->Facebook->fb_api("/PAGE_ID/feed", "POST", array("access_token"=>$app_token,"message"=>"This is a post from PHP."));

我的回复是空的。

这个简单的概念我做错了什么?

1 个答案:

答案 0 :(得分:2)

来自Facebook文档的示例 https://developers.facebook.com/docs/reference/php/facebook-api/

<?php
      // Remember to copy files from the SDK's src/ directory to a
      // directory in your application on the server, such as php-sdk/
      require_once('php-sdk/facebook.php');

      $config = array(
        'appId' => 'YOUR_APP_ID',
        'secret' => 'YOUR_APP_SECRET',
      );

      $facebook = new Facebook($config);
      $user_id = $facebook->getUser();
    ?>
    <html>
      <head></head>
      <body>

      <?
        if($user_id) {

          // We have a user ID, so probably a logged in user.
          // If not, we'll get an exception, which we handle below.
          try {
            $ret_obj = $facebook->api('/'.$pageid.'/feed', 'POST',
                                        array(
                                          'link' => 'www.example.com',
                                          'message' => 'Posting with the PHP SDK!'
                                     ));
            echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';

          } catch(FacebookApiException $e) {
            // If the user is logged out, you can have a 
            // user ID even though the access token is invalid.
            // In this case, we'll get an exception, so we'll
            // just ask the user to login again here.
            $login_url = $facebook->getLoginUrl( array(
                           'scope' => 'publish_stream'
                           )); 
            echo 'Please <a href="' . $login_url . '">login.</a>';
            error_log($e->getType());
            error_log($e->getMessage());
          }   
          // Give the user a logout link 
          echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
        } else {

          // No user, so print a link for the user to login
          // To post to a user's wall, we need publish_stream permission
          // We'll use the current URL as the redirect_uri, so we don't
          // need to specify it here.
          $login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
          echo 'Please <a href="' . $login_url . '">login.</a>';

        } 

      ?>      

      </body> 
    </html>  

嗯,如果没有access_token,你就无法在页面墙上发布内容因为没有访问令牌facebook无法实现谁是谁想要发布&amp;关于你试图通过access_token发帖的时间我认为你做错了,请检查你拥有的权限(你需要publish_stream而不是stream_publish)我会尽快给你一个示例代码