登录facebook突然停止工作

时间:2013-12-27 13:14:59

标签: php facebook facebook-graph-api

突然在我的网络应用程序中使用Facebook登录停止了工作。

以下是我的网站登录页面的代码。我正在使用facebook php sdk,它工作正常,直到5小时后才停止。

我很困惑,想要知道它到底发生了什么,因为自从过去5个小时以来我无法解决它。我删除了连接数据库和查询代码的PHP代码,使其看起来很简单。

我只使用两个范围,即email和publish_stream

以下代码的输出是你好你的fb用户ID是

<?php 

require_once 'include/data.php'; 

//check to see if they're logged in 
if(isset($_SESSION['logged_in'])) { 
    header("Location: index.php"); 
}

$site_url = "http://example.com/facebook.php";  
require_once ('phpsdk/src/facebook.php');

// Create our application instance
$facebook = new Facebook(array(
  'appId'  => 'xxxxxxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'allowSignedRequest' => false
));

// 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;
    }
}

if($user){
    // Get logout URL
    $logoutUrl = $facebook->getLogoutUrl(array(
        'redirect_uri'  => 'http://example.com/logout.php',
        ));
}else{
    // Get login URL
    $loginUrl = $facebook->getLoginUrl(array(
        'scope'         => 'email, publish_stream',
        'redirect_uri'  => $site_url,
        ));
}

// checking

if(!$user) { 
    //echo "<a href='$loginUrl' >Login</a>"; 
    header("Location:$loginUrl");
} else { 
    // echo "<a href='$logoutUrl' >Logout</a><br />"; 
    $f_name = mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $user_info['first_name']); 
    $l_name = mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $user_info['last_name']); 
    $fb_email = $user_info['email']; 
    $fb_uid = $user_info['id']; 
    echo "hello your fb user id is $fb_uid"; // just sample to check
} 
?>

1 个答案:

答案 0 :(得分:0)

FB令牌的生命周期仅有效2小时。来自FB Docs ......

  

检索Facebook访问令牌时,会关联到期时间   通常还会返回访问令牌。如果到期   超过时间,您的应用程序将需要获得新的访问权限   令牌(也将与其关联的新过期时间)。

但是你可以交换令牌并获得一个新的有效期延长60天的东西......就像这样......

 $this->getUrl('graph', '/oauth/access_token'), array(
                    'client_id' => $this->getAppId(),
                    'client_secret' => $this->getAppSecret(),
                    'grant_type'=>'fb_exchange_token',
                    'fb_exchange_token'=>$this->getAccessToken()
                )

有关此问题的更多信息,请访问here

相关问题