2013-08-23 38 views
1

所以,我有以下情形:的PhoneGap +的Facebook + PHP後臺:Facebook的用戶登錄

已經使用Facebook登錄(與JS SDK在一起)來驗證用戶

一個PhoneGap的應用程序是

一個PHP後臺建立,已經有Facebook插件工作。

現在,我的問題是:在我的phonegap應用程序與Facebook進行身份驗證後,我有令牌等,但我需要使用我的PHP後端驗證用戶...有什麼最好的方法來做到這一點, Facebook PHP SDK使用cookie,Phonegap不支持它們?

有沒有辦法將令牌發送到PHP SDK,並使其處理令牌有效性,並真正爲後端用戶創建會話(也就是說,找到我的後端用戶與該電子郵件相關的FB發給我並告訴phonegap應用程序,該用戶是真正的身份驗證,並可以開始使用該應用程序)?

回答

1

所以我最終實現了「Facebook_Volatile」級這樣的:

/** 
* Extends the BaseFacebook class with the intent of NOT using 
* PHP sessions to store user ids and access tokens. 
* @Author Felipe Guaycuru <[email protected]> 
*/ 
class FacebookVolatile extends BaseFacebook 
{ 
    // Stores the shared session ID if one is set. 
    //protected $sharedSessionID; 

    // Stores data non-persistently 
    private $storage = array(); 

    /** 
    * Identical to the parent constructor. 
    * 
    * @param Array $config the application configuration. 
    * @param String $access_token the supplied access token. 
    * @see BaseFacebook::__construct in facebook.php 
    */ 
    public function __construct($config, $access_token) { 
    parent::__construct($config); 
    $this->setAccessToken($access_token); 
    } 

    protected static $kSupportedKeys = 
    array('state', 'code', 'access_token', 'user_id'); 

    /** 
    * Provides the implementations of the inherited abstract 
    * methods. The implementation uses class properties to maintain 
    * a store for authorization codes, user ids, CSRF states, and 
    * access tokens. 
    */ 
    protected function setPersistentData($key, $value) { 
    if (!in_array($key, self::$kSupportedKeys)) { 
     self::errorLog('Unsupported key passed to setPersistentData.'); 
     return; 
    } 

    $this->storage[$key] = $value; 
    } 

    protected function getPersistentData($key, $default = false) { 
    if (!in_array($key, self::$kSupportedKeys)) { 
     self::errorLog('Unsupported key passed to getPersistentData.'); 
     return $default; 
    } 

    return isset($this->storage[$key]) ? 
     $this->storage[$key] : $default; 
    } 

    protected function clearPersistentData($key) { 
    if (!in_array($key, self::$kSupportedKeys)) { 
     self::errorLog('Unsupported key passed to clearPersistentData.'); 
     return; 
    } 

    unset($this->storage[$key]); 
    } 

    protected function clearAllPersistentData() { 
    foreach (self::$kSupportedKeys as $key) { 
     $this->clearPersistentData($key); 
    } 
    } 
} 

因此,在我的PHP後臺,我收到了$access_token,並使用它像這樣:

$FB = new FacebookVolatile(array(
    'appId' => CONFIG_FACEBOOK_APP_ID, 
    'secret' => CONFIG_FACEBOOK_APP_SECRET, 
), $access_token); 

那麼我可以用通常情況下,如果與JS SDK一起使用它一模一樣