Facebook API - 在会话中保存OAuth访问令牌

问题描述:

我试图找到一种方法,一旦使用OAuth授权但存在问题,就会与Facebook API保持连接。我不希望我的应用程序的用户每次想要使用我的应用程序时都必须通过Facebook登录。Facebook API - 在会话中保存OAuth访问令牌

我将oauth访问toekn存储在数据库中,用户通过facebook进行身份验证后,我有"offline_access"权限设置,因此从理论上讲,这应该是可能的。

但是,当使用存储在数据库中的保存的Oauth令牌尝试连接到Facebook API时,我得到了"Uncaught OAuthException: An active access token must be used to query information about the current user."

header("p3p: CP=\"ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV\""); // hack to stop facebook wierd cookie problems 

//instantiate the Facebook library with the APP ID and APP SECRET 
$facebook = new Facebook(array(
    'appId' => 'appid', 
    'secret' => 'secretid', 
    'cookie' => true 
)); 

//Get the FB UID of the currently logged in user 
$user = $facebook->getUser(); 

//if the user has already allowed the application, you'll be able to get his/her FB UID 
if($user) { 
    //get the user's access token 
    $access_token = $facebook->getAccessToken(); 
} else { 
    //see if authorisation already set up in DB 
    $query = mysql_query("SELECT oauth_token FROM PingSocialMediaUsers WHERE oauth_provider = 'facebook' AND clientID = '$clientID'"); 
    $result = mysql_fetch_row($query); 
    $access_token = $result[0]; 
} 

if($access_token) { 

    //check permissions list 
    $permissions_list = $facebook->api(
     '/me/permissions', 
     'GET', 
     array(
      'access_token' => $access_token 
     ) 
    ); 

    //check if the permissions we need have been allowed by the user 
    //if not then redirect them again to facebook's permissions page 
    $permissions_needed = array('publish_stream', 'read_stream', 'offline_access'); 
    foreach($permissions_needed as $perm) { 
     if(!isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1) { 
      $login_url_params = array(
       'scope' => 'publish_stream,read_stream,offline_access', 
       'fbconnect' => 1, 
       'display' => "page", 
       'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] 
      ); 
      $login_url = $facebook->getLoginUrl($login_url_params); 
      header("Location: {$login_url}"); 
      exit(); 
     } 
    } 

    //if the user has allowed all the permissions we need, 
    //get the information about the pages that he or she managers 
    $accounts = $facebook->api(
     '/me', 
     'GET', 
     array(
      'access_token' => $access_token 
     ) 
    ); 

    //add to details database 
    //find the user by ID 
    if ($user != ''){ 
     $query = mysql_query("SELECT * FROM PingSocialMediaUsers WHERE oauth_provider = 'facebook' AND oauth_uid = '$user'"); 
     $result = mysql_fetch_array($query); 

     // If does not exist add to database 
     if(empty($result)){ 
      $query = mysql_query("INSERT INTO PingSocialMediaUsers (oauth_provider, clientID, oauth_uid, username, oauth_token, oauth_secret) VALUES ('facebook', $clientID, $user, '{$accounts['name']}', '$access_token', '')"); 
      $query = mysql_query("SELECT * FROM PingSocialMediaUsers WHERE id = " . mysql_insert_id()); 
      $result = mysql_fetch_array($query); 
     } else { 
      //update the tokens 
      $query = mysql_query("UPDATE PingSocialMediaUsers SET oauth_token = '$access_token', oauth_secret = '' WHERE oauth_provider = 'facebook' AND oauth_uid = '$user'"); 
     } 


    //save the information inside the session 
    $_SESSION['_token'] = $access_token; 
    $_SESSION['accounts'] = $accounts['data']; 
    } 
    $facebookAuth = TRUE; 

Facebook的过程,当其通过应用程序的访问令牌和默认为按照Facebook是2小时一个expires场。

还有其他的因素,为什么其中的access_token可以过期,这里为大家介绍

Ankur Pansari How-To: Handle expired access tokens

的完整细节现在,未来,我们可以谈论offline_access这意味着

It Enables your app to perform authorized requests 
on behalf of the user at any time. By default, 
most access tokens expire after a short time period to ensure applications 
only make requests on behalf of the user when the are actively 
using the application. This permission makes the 
access token returned by our OAuth endpoint long-lived. 

因此,一切都意味着您必须确保您始终使用有效的access_token。有关各种许可的详细信息,请参阅参考链接

Facebook Permissions

+0

你确定你正在使用active_token吗? –

+0

我已经整合了来自“Ankur Pansari如何:处理过期访问令牌”的代码,并会在我未尝试登录时尝试更新状态时看到这是否正常工作。感谢 – LeeTee

+0

是的,它现在似乎正在工作。 – LeeTee