【问题标题】:Post to FB Page as User以用户身份发布到 FB 页面
【发布时间】:2012-03-07 17:30:54
【问题描述】:

所以我有一个 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"
}

但我没有看到墙上的帖子。

当我传递访问令牌时,唉

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

我的回复是空的。

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

【问题讨论】:

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


    【解决方案1】:

    来自 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 无法意识到谁是想要发布的用户以及您尝试通过 access_token 发布的时间,我认为您正在做某事错误请检查您的权限(您需要的是 publish_stream 而不是 stream_publish)我会尽快为您提供示例代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      相关资源
      最近更新 更多