Facebook Graph API - PHP - 无法发布到某些页面

时间:2015-02-03 21:15:23

标签: php facebook facebook-graph-api

问题:为什么会出现此错误,如何解决?

我正在使用Facebook Graph API向我已在特定Facebook群组中创建的特定帖子发布回复/评论。我目前是该群组的成员,可以手动回复/评论我的帖子。

该应用目前在开发人员界面中不是“实时”。

39一些奇怪的帖子中的{p> 80会出现此错误:

Array ( [error] => Array ( [message] => (#200) Permissions error [type] => OAuthException [code] => 200 ) )

访问令牌代码     

//Gets Facebook Token ID for use in facebook-poster.php

session_start();
   $app_id = "...";
   $app_secret = "....";
   $my_url = "......";  // redirect url

    $code = $_REQUEST["code"];

   if(empty($code)) {
     // Redirect to Login Dialog
     $_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
     $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
       . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
       . $_SESSION['state'] . "&scope=publish_stream,publish_actions,read_friendlists,email";


     echo("<script> top.location.href='" . $dialog_url . "'</script>");
   }
if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['state'])) {
     $token_url = "https://graph.facebook.com/oauth/access_token?"
       . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
       . "&client_secret=" . $app_secret . "&code=" . $code;

     $response = file_get_contents($token_url);
     $params = null;
     parse_str($response, $params);
     $longtoken=$params['access_token'];
}

echo $longtoken;

?>

评论发布代码段

            $accessToken = "abc";
            $fbId = array ( '1893939', '919191');


            echo "
            <script>
              window.fbAsyncInit = function() {
                FB.init({
                  appId      : '.....',
                  xfbml      : true,
                  version    : 'v2.2'
                });
              };

              (function(d, s, id){
                 var js, fjs = d.getElementsByTagName(s)[0];
                 if (d.getElementById(id)) {return;}
                 js = d.createElement(s); js.id = id;
                 js.src = \"//connect.facebook.net/en_US/sdk.js\";
                 fjs.parentNode.insertBefore(js, fjs);
               }(document, 'script', 'facebook-jssdk'));
            </script>
            </HEAD>
            <BODY>";


            foreach ($fbId as $ID) { 

                    $i++; //Increment counter for sleep timer.

                    $attachment =  array(
                            'access_token'  => $accessToken,
                            'message'       => "Message",
                    );

                    // set the target url

                    $url = 'https://graph.facebook.com/v2.2/' . $ID . '/comments';

                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
                    curl_setopt($ch, CURLOPT_POST, true);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    $go = curl_exec($ch);
                    echo "Facebook ID:  <a href=http://www.facebook.com/". $ID .">". $ID ."</a> >> Status:  ". $go ."<BR>";
                    curl_close ($ch);

                    $go = json_decode($go, TRUE);

                    echo "JSON Decode 1:  <BR>";
                    print_r($go);

                    if( isset($go['id']) ) {
                        $url = "https://graph.facebook.com/v2.2/{$go['id']}/comments";

                        $attachment =  array(
                                'access_token'  => $accessToken,
                                'message'       => "Bump". rand(0,5000) ."...",
                        );

                        // set the target url
                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, $url);
                        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
                        curl_setopt($ch, CURLOPT_POST, true);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
                        curl_setopt($ch, CURLOPT_HEADER, 0);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                        $comment = curl_exec($ch);
                        curl_close ($ch);
                        $comment = json_decode($comment, TRUE);
                        print_r($comment);
                        echo "<BR><HR>";
                     }

                     if ($i % 20 == 0) { echo "<HR>Sleep<HR><BR>"; sleep(20);  }
            }




        foreach ($fbId as $ID) { 
              echo "<a href=http://www.facebook.com/". $ID .">". $ID ."</a> &nbsp;&nbsp; | &nbsp;&nbsp;";
              if ($i % 10 == 0) { echo "<BR>"; }
            }
}

0 个答案:

没有答案