1

所以我有以下场景:

一个已经使用 Facebook 登录来验证用户身份的 PHP 后端(连同 JS SDK)

正在构建一个 Phonegap 应用程序,它已经让 Facebook 插件工作。

现在,我的问题是:在我的 phonegap 应用程序通过 Facebook 进行身份验证后,我拥有令牌等,但我需要使用我的 PHP 后端对用户进行身份验证......鉴于 Facebook PHP SDK 使用的最佳方法是什么cookie 和 Phonegap 不支持它们?

有没有办法将令牌发送到 PHP SDK,并使其处理令牌有效性并真正在后端为用户创建会话(即找到与 FB 发送给我的电子邮件相关的后端用户并告诉 phonegap用户真正通过身份验证并可以开始使用该应用程序的应用程序)?

4

1 回答 1

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 <guaycuru@gmail.com>
 */
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);

然后就可以$FB正常使用了,和JS SDK一起使用完全一样

于 2013-08-26T13:29:14.773 回答