0

我有一个名为 fbmain.php 的文件,它将对用户进行身份验证。

<?php
    //set facebook application id, secret key and api key here
    $fbconfig['appid' ] = "MY_APP_ID";      
    $fbconfig['secret'] = "MY_APP_SECRET";


    $uid            =   null; //facebook user id

    try{
        include_once "facebook.php";
    }catch(Exception $o){
        echo '<pre>';
        print_r($o);
        echo '</pre>';
    }
    // Create our Application instance.
    $facebook = new Facebook(array(
      'appId'  => $fbconfig['appid'],
      'secret' => $fbconfig['secret'],
      'cookie' => true,
    ));

    //Facebook Authentication part
    $session = $facebook->getSession();

    //Get permission from user
    $loginUrl = $facebook->getLoginUrl(
            array(
            'canvas'    => 1,
            'fbconnect' => 0,
            'req_perms' => 'email,publish_stream,status_update'
            )
    );
    if (!$session) {
        echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
        exit;
    } 
    if ($session) {
        try {
            $uid      =   $facebook->getUser();
        } catch (FacebookApiException $e) {
            echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
            exit;
        }       
    }   

    //signed_request part
    $signed_request = $_REQUEST['signed_request'];
    $secret = $fbconfig['secret'];
    $data = parse_signed_request($signed_request, $secret);
    $fan_page_id = $data['page']['id'];
    $admin_check = $data['page']['admin'];
    $like_check = $data['page']['liked']; //New

        //Get fan page id
    function parse_signed_request($signed_request, $secret) {

        list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

        // decode the data
        $sig = base64_url_decode($encoded_sig);
        $data = json_decode(base64_url_decode($payload), true);

        if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
            error_log('Unknown algorithm. Expected HMAC-SHA256');
            return null;
        }

        // check sig
        $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
        if ($sig !== $expected_sig) {
            error_log('Bad Signed JSON signature!');
            return null;
        }
        return $data;

    }

    function base64_url_decode($input) {
        return base64_decode(strtr($input, '-_', '+/'));
    }       
?>

我已将 fbmain.php 文件包含到我的 index.php 文件中并访问用户数据。
后来我想访问用户生日。所以我在 fbmain.php 文件中添加了“user_birthday”权限引用。下面给出了代码

//Get permission from user    
$loginUrl = $facebook->getLoginUrl(
                array(
                'canvas'    => 1,
                'fbconnect' => 0,
                'req_perms' => 'email,publish_stream,status_update,user_birthday'
                )
        );

index.php 文件的代码如下所示,

<?php
    //index.php file
    include_once "fbmain.php";
    $me = $facebook->api('/me');

    $_SESSION['id'] = $me['id'];
    $_SESSION['name'] = $me['name'];
    $_SESSION['link'] = $me['link'];
    $_SESSION['email'] = $me['email'];

        if($me['birthday'] == null){ ?>
        <script>
            top.location = 'http://www.facebook.com/dialog/oauth?client_id=MY_APP_ID&redirect_uri=MY_REDIRECT_URL&scope=user_birthday';
        </script> 
<?php }else{ ?>
    //Some other codes
<?php } ?>

如果 $me['birthday'] 返回空值,我将用户重定向到请求用户生日的权限窗口。我必须添加这部分,因为我稍后将 user_birthday 添加到权限引用的范围内。
这适用于某些用户并显示“请求权限”窗口,要求访问用户的生日。
但是对于某些用户,它会显示一条 facebook 错误消息(可能是因为我在显示“请求权限”窗口之前尝试访问用户的生日:$me['birthday']==null)
谁能告诉我一个正确的方法来获取用户重新许可访问用户生日?

请注意,此问题仅发生在已在我的应用程序中进行身份验证的用户

4

3 回答 3

0

You can use scope params for you requesting permission. this is code for you want to replace

if ($user) {


 $logoutUrl = $facebook->getLogoutUrl();
} else {


$loginUrl = $facebook->getLoginUrl(
array(
            'scope'         => 'email,publish_stream,user_birthday',

        )


);

if you these this code ,your visitor who visit first time this code request full permision ,and already authendicate for other permisions user comes to this page it request only for birthday permisions . you not need to use

if($me['birthday'] == null){ ?>
    <script>
        top.location = 'http://www.facebook.com/dialog/oauth?client_id=MY_APP_ID&redirect_uri=MY_REDIRECT_URL&scope=user_birthday';
    </script> 
<?php }else{ ?>
//Some other codes

于 2011-09-07T11:10:21.973 回答
0

我不懂 php...bt 检查 FB.ui

https://developers.facebook.com/docs/reference/dialogs/oauth/

在这里,您有 oAuth 对话框来询问用户权限。

于 2011-09-07T09:25:36.783 回答
0

您知道 facebook 将权限参数从req_perms更改为getLoginUrl ()函数中的范围。尝试scope,可能有效。

https://github.com/facebook/php-sdk/issues/381

于 2011-09-07T09:25:04.957 回答