Facebook Graph API - PHP - Can't post to certain pages

824

Question: Why is it giving this error and how do I resolve it?

I am using theFacebook Graph API to post a reply/comment to specific posts that I have already created in specific Facebook groups. I am a currently a member of the group and can reply/comment manually to my post.

The app is not currently "live" in the developer interface.

39 of the80 some odd posts give this error:

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

Access Token Code

//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;

?>

Comment Posting Snippet

            $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>"; }
            }
}

People are also looking for solutions to the problem: java - Android Spinner selected item not showing

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.