无法让FB授权正常工作

时间:2012-04-06 19:21:27

标签: facebook-graph-api oauth-2.0

http://apps.facebook.com/karmabalance/

我正在尝试在我的FB应用程序上运行登录和授权,但它似乎做了有趣的事情。基本上我想要的只是当用户点击我的应用程序时,我希望fb进行检查以确保他们已登录并批准了应用程序,之后我需要程序检查数据库以查看是否它们已经是用户FB ID在数据库中的记录。如果他们没有记录,我希望程序将用户发送到创建帐户。允许用户输入用户名的页面。如果有记录,我想将用户发送到应用主页。

我以为我得到了它的工作,但它仍然不对,我无法弄清楚为什么。这是我的代码。我确信还有一种更好的方法可以做到这一点,我是一个新手,所以我试图在我走的时候弄清楚这一切。任何帮助,将不胜感激。

由于

当用户点击我的应用时,它会转到此index.php页面。

<?php 
require 'config.php';

session_start();

$usercheck = $user_profile['id'];

$result = mysql_query("SELECT FBID FROM PLAYER WHERE (FBID = '$usercheck') ");

if ($facebook->getUser()) 
{ 

    if(mysql_num_rows($result))
    {
        header('location: Home.php');
    } else 
    { ?>
         <p>Please choose a Username for yourself. </p>
            <form action='includes/signup.php' method='post'>
            <fieldset style="width:600px";>
            <label>Username</label>
            <input name='username' type='text' value='<? if ($facebook->getUser()) 
                    {echo'Choose a Username';} ?>' />

            <input name='submit' type='submit' value='submit' />
            </fieldset>
            </form>
     <? }
} else 
  { ?>
   <p>Sign up with Facebook <fb:login-button perms='email'> Connect</fb:login-button>

It only takes a few seconds</p>

<div id='fb-root'></div>

      <script src='http://connect.facebook.net/en_US/all.js'></script>

      <script>

         FB.init({ 

            appId:'334230339967350', cookie:true, 

            status:true, xfbml:true 

         });

         FB.Event.subscribe('auth.login', function(response) {

        window.location.reload(); //will reload your page you are on

      });

      </script>
  <? }

?>

配置文件:

<?php 
//Facebook Configuration
require 'facebook-php-sdk/src/facebook.php';

$app_id = "xxxxxxxxxxxx";
$app_secret = "xxxxxxx";
$facebook = new Facebook(array(
        'appId' => $app_id,
        'secret' => $app_secret,
        'cookie' => true
));

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

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}


//SQL Configuration

 // i start most if not all pages with this depending on what Im using it for.

    $host = 'localhost'; // host name OR IP

    $username = 'security';//username

    $pass = 'blockedout'; //password

    $dbname    = 'Security'; // Database Name

    $conn = mysql_connect($host, $username, $pass) or die(mysql_error());

    if ($conn)

    {

        mysql_select_db($dbname) or die(mysql_error());

    }

    else

    {

       echo 'Connection failed.';

    } // The above code connects to your database or errors if it cannot connect.

// Again this is simple, security is your own priority.

//GLOBAL VALUES??

$usercheck = $user_profile['id'];

$usernamequery = mysql_query("SELECT UserName FROM PLAYER WHERE (FBID = '$usercheck') ");
$username = mysql_fetch_array($usernamequery);

$levelquery = mysql_query("SELECT LevelID FROM PLAYER WHERE (FBID = '$usercheck') ");
$level = mysql_fetch_array($levelquery);


$result = mysql_query("SELECT FBID FROM PLAYER WHERE (FBID = '$usercheck') ");


?>

这是signup.php页面

<?

require_once '../config.php';



//here you could add checks for any empty fields using (!($_POST['first_name']))

$first_name = $user_profile['first_name']; // this line will collect our information from the

// field in our form that has the facebook first_name in it.

$last_name = $user_profile['last_name']; // same as above

$email = $user_profile['email']; //same as above

$id = $user_profile['id']; //same as above

$username1 = $_POST['username'];

$query = mysql_query
        ("
        INSERT INTO PLAYER (FirstName, LastName, EMail, FBID, UserName, Status, LevelID, Cash, LifePoints, RespectPoints, ReputationPoints, UpgradePoints, HealthPercent) 
        VALUES ('$first_name', '$last_name', '$email', '$id', '$username1', '1', '1', '25000', '3', '3', '3', '20', '100')
        ") 
        or die(mysql_error());

// The query will insert our fields in to the database as the above line shows, make 

//sure your database table headers are exactly correct otherwise this will not work



// You can now either send an email or if you wanted header to a new page. This is 

//up to you. Tutorials on google will show you how to do this part

if($query){

header('location: ../Home.php');

}else {

echo 'error adding to database';    

}

?>

1 个答案:

答案 0 :(得分:0)

您缺少用户的oauth令牌。这就是你要拉的用户的句柄。 如果你仍然困惑,请告诉我。

相关问题