2013-03-16 99 views
2

因此,如果我想用過期的訪問令牌來發現錯誤,我必須檢查這個出 https://developers.facebook.com/blog/post/2011/05/13/how-to--handle-expired-access-tokens/ 和好,但它的一點點不清楚。在我的數據庫訪問令牌

// known valid access token stored in a database 
    $access_token = "YOUR_STORED_ACCESS_TOKEN"; 

所以我必須讓最後一個用戶在我的數據庫中訪問令牌?

$code = $_REQUEST["code"]; 

    // If we get a code, it means that we have re-authed the user 
    //and can get a valid access_token. 
    if (isset($code)) { 

所以如果我得到$_REQUEST["code"]這意味着用戶有訪問令牌?

我的應用程序打印用戶喜歡,如果用戶更改密碼,我必須獲得新的訪問令牌。我應該如何驗證這些令牌?在這種情況下,facebook文檔對我來說確實不太清楚;/

回答

1

您可以通過異常處理來完成。如果您的訪問令牌已過期或無效,則會向您發送錯誤消息並向您提供下面提到的消息。

public function getUserInfo($access_token) 
    {  
       try { 
         $user_info = $this->facebook->api(
         '/me', 
         'GET', 
           array('access_token' =>$access_token) 
        ); 
        } 
        catch(Exception $e){ 
         $message = $e->getMessage(); 
          if ((strpos($message, 'Error validating access token') !== false) || 
           (strpos($message, 'Invalid OAuth access token') !== false) || 
           (strpos($message, 'An active access token must be used') !== false) 
          ) { 
           echo 'Your custom Message'; 
          } 
        } 
} 

您可以將您的訪問令牌傳遞給您的函數,並且比您檢查是否執行上述異常處理。

希望它可以幫助你。

通過這種方式,您可以得到擴展的訪問令牌

/** 
    * Getting User Acess Tocken , extended it and save it in to database...... 
    */ 
    public function getAccessToken($user_id,$fb_account_id) 
    { 
     $access_token=$this->facebook->getAccessToken();    
     $extended_access_token=$this->getExtendedAccessToken($access_token);     
     /** 
     * To save Access tocken and other necessary option 
     */ 
     $usr_bus_model = new Application_Model_UserBusinessAccounts;     
     $usr_bus_model->loadAccount($user_id,'Facebook',(int)$fb_account_id,false);   
     $usr_bus_model->details=$extended_access_token; 
       if(!empty($extended_access_token)) 
       { 
        $usr_bus_model->status= 'active'; 
       } 
     $usr_bus_model->save(); 

     return $extended_access_token; 
    } 

    public function getExtendedAccessToken($access_token) 
     {  

      $token_url="https://graph.facebook.com/oauth/access_token"; 
      $params=array('client_id'=>self :: appId,'client_secret'=>self :: appSecretId,'grant_type'=>'fb_exchange_token','fb_exchange_token'=>$access_token); 

       $response = $this->curl($token_url,$params); 
       $response = explode ('=',$response); 
       $response = explode ('&',$response[1]); 
       $response = $response[0];  
       return $response; 

     } 

我上述兩種功能做出。所以,只要你需要使用訪問令牌,只需調用擴展訪問令牌並將其保存到你的數據庫。所以你的訪問令牌永遠不會過期。

+0

非常感謝,這是可以理解的。所以如果我發現了一些錯誤,我必須得到新的訪問令牌。如果我想在牆上張貼東西,我需要訪問令牌,那麼我必須將令牌保存在我的數據庫中,是的,或者我應該始終在我的數據庫中保留訪問令牌? – robinloop 2013-03-16 11:42:07

+0

是的,如果你發現一些錯誤比你需要獲得新的訪問令牌。並把它保存到你的數據庫只需替換舊的。多數民衆贊成它..我已經完成了上述相同的方式.. – 2013-03-16 11:44:43

+0

它更好地保持60天令牌在我的分貝,對不對?所以當用戶登錄時,立即獲得新的60天令牌? – robinloop 2013-03-16 11:52:29