Facebook publish_stream ends in a loop

I have the following code, which works great if in the previous part of the application, the user accepts an application request for publish_stream

.

# The facebook library
require_once("/var/www/facebook-php-sdk-master/src/facebook.php");

# Create facebook object
$config = array();
$config['appId'] = 'appId_here';
$config['secret'] = 'secret_here';
$config['fileUpload'] = false; // optional

$facebook = new Facebook($config); 

$user_id = $facebook->getUser();

if ($user_id) {

    try {

        $user_profile = $facebook->api('/me','GET');

        #check permissions
        $api_call = array(
                'method' => 'users.hasAppPermission',
                'uid' => $user_id,
                'ext_perm' => 'publish_stream'
        );

        #set to true if true...
        $can_offline = $facebook -> api( $api_call );

        #is it true?
        if( $can_offline ) {

            $post =  array(
                'message' => 'post_a_message'
            );

            $facebook->api('/' . $_GET["id"] . '/feed', 'POST', $post);

        } else {

            // can't post message - don't have permission

        }

    } catch (FacebookApiException $e) {

        error_log($e);
        exit;

    }

} else {

    error_log("user not logged in");
    exit;

}

      

To try and solve this problem, I tried to paste the following code in else statement

, which is currently in the code above, contains a comment// can't post message - don't have permission

The code I was trying to put into this else

was the following:

$loginUrl = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
header("Location: ".$loginUrl);

      

This works as long as the user agrees to allow my application publish_stream

. However, if the user doesn't accept, my app will keep asking the user to accept publish_stream

. How can I stop this cycle if the user chooses not to accept?

+3


source to share


3 answers


Here is the working code: Please check it out:

Page title: events.php

You can see $redirect_uri = https://localhost/facebook_page/events.php

it goes back to the same page.



<?php

$facebook_appid         = "your appid";                     // Facebook appplication id
$facebook_secret        = "your app secret";                // Facebook secret id
$redirect_uri           = "https://localhost/facebook_page/events.php";   // return url to our application after facebook login ## should be SAME as in facebook application
$scope                  = "publish_stream"; // User permission for facebook

$profile_id             = "profile_id";// Where do you want to post it(profile id - It is a number)

$code                   = $_REQUEST["code"]?$_REQUEST["code"]:"";

if(empty($code)) {
    $_SESSION['state']  = rand(); // CSRF protection
    $dialog_url         = "https://www.facebook.com/dialog/oauth?client_id=". $facebook_appid . "&redirect_uri=" . urlencode($redirect_uri) . "&state=". $_SESSION['state'] . "&scope=".$scope;
    header("location:".$dialog_url);
}

if($_SESSION['state'] && ($_SESSION['state'] == $_REQUEST['state'])) {
    $token_url          = "https://graph.facebook.com/oauth/access_token?". "client_id=" . $facebook_appid . "&redirect_uri=" . urlencode($redirect_uri). "&client_secret=" . $facebook_secret . "&code=" . $code;
    $response           = @file_get_contents($token_url);

    $params             = null;
    parse_str($response, $params);

    $access_token       = $params['access_token'];


}

?>

<!-- Here you can use 
      message, picture, link, name, caption, description, source, place, tags 
     as input fields-->

<form enctype="multipart/form-data" method="POST" action="https://graph.facebook.com/<?php echo $profile_id;?>/feed?access_token=<?php echo $access_token; ?>">
    <input type="text" name="message" value="test" />
    <input type="submit" name="submit" value="Submit" />
</form>

      

You can also post it using jquery.

+2


source


As far as I remember, $facebook -> getLoginUrl

can take a parameter cancel_url

that contains a link where the user should be redirected if he does not give permission to use your application.

So the code will be something like this



$login_url = $facebook -> getLoginUrl( array(
'scope' => 'publish_stream',
'cancel_url' => YOUR_LINK_HERE
));

      

+4


source


I am using the following code to check if the user has allowed to post permissions or not:

$permissions = $facebook->api('me/permissions');
if( array_key_exists('publish_stream', $permissions['data'][0]) ) {
    //Continue with posting on users wall
} else {
    //Continue without posting on users wall
}

      

+1


source







All Articles