错误Facebook应用程序,权限接受,但......第一次不起作用

时间:2014-01-14 13:18:50

标签: php mysql database facebook permissions

我的数据库存在一个很大的问题,即我在Facebook上的应用程序的许可。

当用户打开应用程序(index.php)并接受权限时,应用程序转到另一个页面(game.php)...当game.php打开时,脚本检查数据库以查看是否Facebook的ID已经存在,如果没有数据库成为用户。

当game.php第一次打开时,页面会生成此错误:

OAuthException: An active access token must be used to query information about the current user.
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in game.php on line 67 check1Error: Duplicate entry '0' for key 'UIDfacebook' account

可能是因为facebook的ID尚未准备好在数据库中保存,为什么?权限是接受...如果我刷新页面,页面将正常工作,数据库保存ID,用户名....

INDEX.PHP(REQUEST)

<?php
require('client/facebook.php');

$app_id = '***';
$app_secret = '***';
$canvas_page = "https://apps.facebook.com/***";
$canvas_url = "****";

$code = $_REQUEST['code'];

if(!$code){
$display= 'page';
$scope= 'offline_access, publish_stream, user_about_me, user_likes, email'; 
$redirect_url = 'game.php';
$oauth_url    = 'https://www.facebook.com/dialog/oauth?canvas=1&client_id='.$app_id.'&display='.$display.'&redirect_uri='.urlencode($redirect_url).'&scope='.$scope;
$config = array('appId'  => $app_id,'secret' => $app_secret,'cookie' => true,'domain' => true);
$facebook_client = new Facebook($config);

echo "<script type=\"text/javascript\">top.location.href = \"".$oauth_url."\";</script>";
}
?>

GAME.PHP

<?php
require 'client/facebook.php';
require('client/databaselogin.php');
///////////////////
$app_id = "***";
$secret = "***";
$app_url = "https://apps.facebook.com/***/";
//Create our Application instance
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $secret,
'cookie' => 'true',
));
// Get User ID
$user = $facebook->getUser(); 
//Do we have a user?
if(!$user) {
    $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=> $app_url));
    //redirect the url to prompt for login and permissions
    } else {
        try{
            //Proced knowing you have a logged in user who's autenticated
            $user_profile = $facebook->api('/me');
            $name = $user_profile['name'];
            $id = $user_profile['id'];
            $first_name = $user_profile['first_name'];
            $last_name = $user_profile['last_name'];
            $link = $user_profile['link'];
            $username = $user_profile['username'];
            $gender =  $user_profile['gender'];
            $locale = $user_profile['locale'];          
        } catch (FacebookApiException $e) {
            echo ($e);
            $user = null;
        }
}


$checkUIDdb = "";
$connection = "wait";

$id = $user_profile['id'];
$username = $user_profile['username'];
$goldDB = "0";
$user_type = "A";

///connect to the database

$connessione = mysql_connect($host, $userdb, $passdb)
        or die("error: " . mysql_error());
    print ("connect");

    echo $connection = "ok";
    echo $id;

///

$fb_id = $user_profile['id'];
$locale = $user_profile['locale'];
$con=mysqli_connect($sdb,$userdb,$passdb,$db);

if ($connection == "ok") 
{
//Checking User Data @ WT-Database
$check1_task = "SELECT * FROM `tabletest` WHERE `UIDfacebook` = " . $fb_id . " LIMIT 0, 30 ";
$check1_res = mysqli_query($con, $check1_task);
$checken2 = mysqli_fetch_array($check1_res);
print $checken2;

//If User does not exist @ WT-Database -> insert
if (!($checken2)) {
$add = "INSERT INTO tabletest (id,UIDfacebook, username, goldDB, user_type) VALUES ('', '$fb_id', '$username', '$goldDB', '$user_type')";
mysqli_query($con, $add);
if (!$check1_res) {
    echo "check1";
    printf("Error: %s\n", mysqli_error($con));
}
} 
//Double-check, User won't be able to load app on failure inserting to database 
if (!($checken2)) {

    //echo "Excuse us " . $first_name . ". Something went terribly wrong! Please try again later!";

} 
}
    //Close off the MySQL connection to save resources.
 ?>


<head>
</head> 
</body>
    </script>
    MY APPLICATION
    </script>
</body> 
</html> 
有人知道会发生什么事吗?

非常感谢

编辑:

我尝试将此脚本用于GAME.PHP:

<?php
require 'client/facebook.php';
$app_id = "aaa";
$secret = "aaa";
$app_url = "http://app.facebook.com/aaa/";

// Create our Application instance
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $secret,
));

// Get User ID
$user = $facebook->getUser(); 

//Do we have a user?
if(!$user) {
    $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=> $app_url));
    //redirect the url to prompt for login and permissions
    echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
    } else {
        try{
            //Proced knowing you have a logged in user who's autenticated
            $user_profile = $facebook->api('/me');
            $name = $user_profile['name'];
            $id = $user_profile['id'];
            $first_name = $user_profile['first_name'];
            $last_name = $user_profile['last_name'];
            $link = $user_profile['link'];
            $username = $user_profile['username'];
            $gender =  $user_profile['gender'];
            $locale = $user_profile['locale'];


        } catch (FacebookApiException $e) {
            echo ($e);
            $user = null;
        }
}
echo $username;
echo $id;
echo $user_profile;
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk example1 </title>
<style>
body {
    font-family:Arial, Helvetica, sans-serif;
}
a{
    text-decoration:none;
}

但是第一次,错误是类似的?

OAuthException: Error validating access token: The session was invalidated explicitly using an API call.

如果我刷新页面,问题就会消失......

0 个答案:

没有答案