2012-04-06 73 views
0

http://apps.facebook.com/karmabalance/无法获得FB授权正常工作

我试图获取登录和授权对我的FB应用程序的工作,但似乎做的有趣的东西。基本上所有我想要的是,当用户点击我的应用程序,我想fb做他们的检查,以确保他们已登录并已批准该应用程序,之后,我需要该程序来检查数据库,看看是否它们已经是用户FB ID在数据库中的记录。如果他们没有记录,我希望程序发送用户到创建acct。页面,使用户可以输入用户名。如果有记录,我想将用户发送到应用程序主页。

我想我已经开始工作了,但它仍然不正确,我不明白为什么。这是我的代码。我相信还有更好的方法来做到这一点,我是一个新手,所以我试图在我走的时候把这一切都弄清楚。任何帮助,将不胜感激。

感谢

当用户点击我的应用程序它进入这个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';  

} 

?> 

回答

0

你缺少用户OAuth令牌。那就是你想要拉的用户的句柄。 如果你仍然混淆,让我知道。

+0

谢谢,是啊,我以为我是这样做的 '//获取用户ID $ user = $ facebook-> getUser(); 如果($用户){ 尝试{// 继续了解你一登录谁是验证用户。 $ user_profile = $ facebook-> api('/ me'); (FacebookApiException $ e){ error_log($ e); $ user = null; }} ' 也许你可以点我在正确的方向开始研究它吗? – 2012-04-07 23:32:05

相关问题