3

我有以下代码,只要在应用程序的前一部分中,用户接受对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;

}

为了解决这个问题,我尝试将以下代码插入到else statement上面代码中当前仅包含注释的代码中// can't post message - don't have permission

我试图插入的代码else是这样的:

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

只要用户接受允许我的应用程序使用publish_stream. 但是,如果用户不接受,我的应用程序会不断要求用户接受publish_stream。如果用户决定不接受,我该如何阻止该循环的发生?

4

3 回答 3

4

据我记得,$facebook -> getLoginUrl可以采用参数cancel_url,其中包含链接,如果用户不授予您的应用权限,则应将其重定向到该链接。

所以代码将是这样的

$login_url = $facebook -> getLoginUrl( array(
'scope' => 'publish_stream',
'cancel_url' => YOUR_LINK_HERE
));
于 2013-02-15T09:13:37.060 回答
2

这是工作代码:请检查:

页面名称:events.php

您可以看到$redirect_uri = https://localhost/facebook_page/events.php它正在返回到同一页面。

<?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>

您也可以使用 jquery 发布它。

于 2013-02-20T06:57:20.400 回答
1

我使用以下代码检查用户是否允许发布权限:

$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
}
于 2013-02-19T07:44:02.047 回答