如何在点击&#34后检查用户是否为管理员;继续为"

时间:2016-11-03 10:49:33

标签: php facebook-graph-api

我正在创建一个facebook测验,但我几天都遇到了以下问题。当用户启动游戏并点击"继续作为" (Facebook要求获取一些用户信息的屏幕)我想检查进入游戏的用户是否是管理员。到目前为止,当用户允许分享他的信息时,他以用户身份进入游戏。我尝试了几种不同的解决方案,我尝试过发送几种不同的api,我已经阅读了大量的例子,但到目前为止我还没有运气。在developers.facebook.com,我的应用程序是我设置了manage_pages,我的用户配置文件被选为"管理员"。这是我的login.php脚本(注释掉的代码是我检查过的解决方案)。任何帮助将不胜感激。

    <?php 

    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }

    include_once ('fb_sdk/src/Facebook/autoload.php');

    $fb = new Facebook\Facebook([
      'app_id' => '*******',
      'app_secret' => '*******',
      'default_graph_version' => 'v2.8',
      ]);

    $helper = $fb->getCanvasHelper();

    try {
        if (isset($_SESSION['facebook_access_token'])) {
        $accessToken = $_SESSION['facebook_access_token'];
        } else {
            $accessToken = $helper->getAccessToken();
        }
    } catch(Facebook\Exceptions\FacebookResponseException $e) {
        // When Graph returns an error
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
     }
    if (isset($accessToken)) {

        if (isset($_SESSION['facebook_access_token'])) {
            $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
        } else {
            $_SESSION['facebook_access_token'] = (string) $accessToken;

            // OAuth 2.0 client handler
            $oAuth2Client = $fb->getOAuth2Client();
            // Exchanges a short-lived access token for a long-lived one
            $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
            $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
            $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
        }
        // validating the access token
        /*try {
            $request = $fb->get('/me');
        } catch(Facebook\Exceptions\FacebookResponseException $e) {
            // When Graph returns an error
            if ($e->getCode() == 190) {
                unset($_SESSION['facebook_access_token']);
                $helper = $fb->getRedirectLoginHelper();
                $loginUrl = $helper->getLoginUrl('https://apps.facebook.com/facebookquiz/', $permissions);
                echo "<script>window.top.location.href='".$loginUrl."'</script>";
                exit;
            }
        } catch(Facebook\Exceptions\FacebookSDKException $e) {
            // When validation fails or other local issues
            echo 'Facebook SDK returned an error: ' . $e->getMessage();
            exit;
        }*/
        // getting basic info about user
        try {
            $profile_request = $fb->get('/me?fields=first_name, last_name, email');
            $profile = $profile_request->getGraphNode()->asArray();
        } catch(Facebook\Exceptions\FacebookResponseException $e) {
            // When Graph returns an error
            echo 'Graph returned an error: ' . $e->getMessage();
            unset($_SESSION['facebook_access_token']);
            echo "<script>window.top.location.href='https://apps.facebook.com/facebookquiz/'</script>";
            exit;
        } catch(Facebook\Exceptions\FacebookSDKException $e) {
            // When validation fails or other local issues
            echo 'Facebook SDK returned an error: ' . $e->getMessage();
            exit;
        }
        // priting basic info about user on the screen


        $first_name = $profile['first_name'];
        $last_name = $profile['last_name'];
        $email = $profile['email'];
        $fbUserId = $profile['id'];

        print_r ($profile);

        include_once('database/dbInsert.inc');
        include_once('database/db_select.inc');

        $fbUser = getUserByFBID($fbUserId);

        if($fbUser == false){
            $userID = insertUserInfo($fbUserId,$first_name, $last_name, $email);
            $_SESSION['user_id'] = $userID;
        }else{
            $_SESSION['user_id'] = $fbUser->Id;
        }


        header("Location: index.php");

        //redirect if is administrator
    } else {
        $helper = $fb->getRedirectLoginHelper();
        $permissions = ['email', 'public_profile', 'publish_actions', 'manage_pages']; // optionnal 'user_managed_groups'
        $loginUrl = $helper->getLoginUrl('https://apps.facebook.com/facebookquiz/login.php', $permissions);
        echo "<script>window.top.location.href='".$loginUrl."'</script>";
    }
    ?><?php 

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

include_once ('fb_sdk/src/Facebook/autoload.php');

$fb = new Facebook\Facebook([
  'app_id' => 'app-if',
  'app_secret' => 'app-secret',
  'default_graph_version' => 'v2.8',
  ]);

$helper = $fb->getCanvasHelper();

try {
    if (isset($_SESSION['facebook_access_token'])) {
    $accessToken = $_SESSION['facebook_access_token'];
    } else {
        $accessToken = $helper->getAccessToken();
    }
} catch(Facebook\Exceptions\FacebookResponseException $e) {
    // When Graph returns an error
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
 }
if (isset($accessToken)) {

    if (isset($_SESSION['facebook_access_token'])) {
        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    } else {
        $_SESSION['facebook_access_token'] = (string) $accessToken;

        // OAuth 2.0 client handler
        $oAuth2Client = $fb->getOAuth2Client();
        // Exchanges a short-lived access token for a long-lived one
        $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
        $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    }
    // validating the access token
    /*try {
        $request = $fb->get('/me');
    } catch(Facebook\Exceptions\FacebookResponseException $e) {
        // When Graph returns an error
        if ($e->getCode() == 190) {
            unset($_SESSION['facebook_access_token']);
            $helper = $fb->getRedirectLoginHelper();
            $loginUrl = $helper->getLoginUrl('https://apps.facebook.com/facebookquiz/', $permissions);
            echo "<script>window.top.location.href='".$loginUrl."'</script>";
            exit;
        }
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }*/
    // getting basic info about user
    try {
        $profile_request = $fb->get('/me?fields=first_name, last_name, email');
        $profile = $profile_request->getGraphNode()->asArray();
    } catch(Facebook\Exceptions\FacebookResponseException $e) {
        // When Graph returns an error
        echo 'Graph returned an error: ' . $e->getMessage();
        unset($_SESSION['facebook_access_token']);
        echo "<script>window.top.location.href='https://apps.facebook.com/facebookquiz/'</script>";
        exit;
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
    // priting basic info about user on the screen


    $first_name = $profile['first_name'];
    $last_name = $profile['last_name'];
    $email = $profile['email'];
    $fbUserId = $profile['id'];

    print_r ($profile);

    include_once('database/dbInsert.inc');
    include_once('database/db_select.inc');

    $fbUser = getUserByFBID($fbUserId);

    if($fbUser == false){
        $userID = insertUserInfo($fbUserId,$first_name, $last_name, $email);
        $_SESSION['user_id'] = $userID;
    }else{
        $_SESSION['user_id'] = $fbUser->Id;
    }

    /*$getPages = (new Facebook\FacebookRequest(
      $session,
      'GET',
      '/me/accounts'
    ))->execute()->getGraphObject()->asArray();

    $pageID = $getPages['data'][0]->id;
    $pageAccessToken = $getPages['data'][0]->accessToken;

    (new FacebookRequest(
    $session,
    'POST',
    '/' . $pageID . '/feed',
    array(
        'accessToken' => $pageAccessToken;
        'message' => 'Facebook app';
        )

    ))->execute();*/


    //foreach ($getPages['data'] as $key) {

        //print_r($getPages);

        /*echo $key->id;
        echo "<br>";
        echo $key->name;
        echo "<br>";*/

    //}

    /*$response = $request->execute();
    $graphObject = $response->getGraphObject();*/
/*$request = new FacebookRequest(
  $_SESSION,
  'POST',
  '/883093058488924/roles',
  array (
    'user' => '123456789',
    'role' => 'administrators',
  )
);
$response = $request->execute();
$graphObject = $response->getGraphObject();

var_dump($graphObject);*/


    header("Location: index.php");

    //redirect if is administrator

    // Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']
} else {
    $helper = $fb->getRedirectLoginHelper();
    $permissions = ['email', 'public_profile', 'publish_actions', 'manage_pages']; // optionnal 'user_managed_groups'
    $loginUrl = $helper->getLoginUrl('https://apps.facebook.com/facebookquiz/login.php', $permissions);
    echo "<script>window.top.location.href='".$loginUrl."'</script>";
}
?>

0 个答案:

没有答案