2013-05-12 107 views
0

嗨我有一个问题,我的Facebook数据(登录数据)存储到我的个人SQL数据库。 在这里您可以找到我的代码:我首先使用索引页,然后使用登录页面将我的Facebook数据登录存储在我的数据库中。在我的登录页面中,我重定向到了我的user_page。但是,当我重定向到我user_page Facebook的数据没有存储和我得到的链接https://localhost/project/login.php?error_code=901&error_message=This+app+is+in+sandbox+mode.++Edit+the+app+configuration+at+http%3A%2F%2Fdevelopers.facebook.com%2Fapps+to+make+the+app+publicly+visible.&state=7504294781f7ae045299820cb5c40ead#_=_facebook-php-sdk不想在我的数据库中存储数据

索引页代码

<?php 

require 'facebook-php-sdk/src/facebook.php'; 

define('APP_URL', 'https://localhost/project/login.php'); 
define('APP_PERMISSIONS', ''); 

// Create our Application instance (replace this with your appId and secret). 
$facebook = new Facebook(array(
    'appId' => 'APPID', 
    'secret' => 'SECRET', 
    'cookie' => true 
)); 

$loginUrlParams = array(
    'scope' => APP_PERMISSIONS, 
    'redirect_uri' => APP_URL, 
); 

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

// We may or may not have this data based on whether the user is logged in. 
// 
// If we have a $user id here, it means we know the user is logged into 
// Facebook, but we don't know if the access token is valid. An access 
// token is invalid if the user logged out of Facebook. 

if ($user) { 
    try { 
    // Proceed knowing you have a logged in user who's authenticated. 
    $fbuid = $facebook->getUser(); 
    $user_profile = $facebook->api('/me'); 

    // header('Location: user_page.php'); 
    } catch (FacebookApiException $e) { 
    error_log($e); 
    $user = null; 
    header('Location: ' . $facebook->getLoginUrl($loginUrlParams)); 
     exit; 
    } 
    } 
    else 
    { 
    header('Location: ' . $facebook->getLoginUrl($loginUrlParams)); 
    exit; 
    } 
?> 

登录页面代码:

<?php 

define("db_DATABASE", "phples"); 
define("db_SERVER", "localhost"); 
define("db_USER", "root"); 
define("db_PASS", ""); 

$conn=new mysqli(db_SERVER,db_USER,db_PASS,db_DATABASE); 

require 'facebook-php-sdk/src/facebook.php'; 

    // Create our Application instance (replace this with your appId and secret). 
    $facebook = new Facebook(array(
    'appId' => 'APPID', 
    'secret' => 'SECRET', 
    'cookie' => true)); 


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

if($user){ 

$user_profile = $facebook->api('/me'); 

    $query = mysql_query("SELECT * FROM facebook_users WHERE oauth_provider = 'facebook' AND oauth_uid = ". $user_profile['id']); 
    $result = mysql_fetch_array($query); 

    if(empty($result)){ 
     $query = mysql_query("INSERT INTO facebook_users (oauth_provider, oauth_uid, username) VALUES ('facebook', {$user_profile['id']}, '{$user_profile['name']}')"); 

     header('Location: user_page.php'); 

           }else{ 
     header('Location: user_page.php'); 
     } 


      }else{ 

     header('Location: logout.php'); 


     } 

      ?> 

用户页面代码:

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

// Create our Application instance (replace this with your appId and secret). 
$facebook = new Facebook(array(
    'appId' => 'APPID', 
    'secret' => 'SECRET', 
    'cookie' => true 
)); 

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

// We may or may not have this data based on whether the user is logged in. 
// 
// If we have a $user id here, it means we know the user is logged into 
// Facebook, but we don't know if the access token is valid. An access 
// token is invalid if the user logged out of Facebook. 

if ($user) { 
    try { 
    // Proceed knowing you have a logged in user who's authenticated. 
    $fbuild = $facebook->getUser(); 
    $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) { 
     $paramsout = array('next'=>'http://localhost/project/logout.php'); 
     $logoutUrl = $facebook->getLogoutUrl($paramsout); 
} 


?> 

回答

0

错误说明您的应用程序可能存在什么问题。因为它说

ERROR_MESSAGE =此+应用程式是+在+沙箱+模式。++编辑+的+应用+配置在+ + HTTP%3A%2F%2Fdevelopers.facebook.com%2Fapps +到+化妆+手机应用程式+公开+可见。

您正在沙箱模式下运行您的应用程序。在这种模式下,只有开发人员,测试人员和其他与应用程序相关的人才能使用它,但除此之外没有其他人使用。因此,请更改您的应用设置并禁用sandbox mode

编辑

根据您的评论的错误状态

一个或多个指定的网址不被应用的设置允许的。它必须将网站网址或帆布URL匹配,或者域必须应用的领域

之一的的子域,因为它说,你应该在你的Website with Facebook Login您的应用程序设置中添加https://localhost/,使Facebook可能重定向回到你的页面。

+0

沙盒问题已解决,但我收到消息: **应用程序的设置不允许使用一个或多个给定的URL。它必须与网站URL或Canvas URL匹配,或者域必须是其中一个应用程序域的子域。**我有localhost/PHP/project/test2.php作为主页面:我将这个链接放在我的脸书开发应用程序设置? – Glenda 2013-05-13 08:06:33

+0

@Glenda我已经更新了答案,请检查 – 2013-05-13 08:10:48

+0

我已将http://localhost/PHP/project/test2.php放在我的网站domein的Facebook应用页面上,但没有https。我也与其他人分享这个appid和秘密。 – Glenda 2013-05-13 10:24:12