【问题标题】:Need to Authenticate the Users using Twitter, Google, Facebook Apis through oauth plugin?需要通过 oauth 插件使用 Twitter、Google、Facebook API 对用户进行身份验证?
【发布时间】:2013-04-26 19:41:57
【问题描述】:

首先我想知道如何使用 oauth 工作。我们需要在这个插件中传递什么以及这个插件将返回什么。我们是否必须为不同的php框架定制插件。我已经看到它们是针对不同框架的 oauth 的不同扩展,这是为什么呢?

我需要在 yii 框架中对使用社交网络的用户进行身份验证,并且我已经集成了 yii 的 eouath 扩展以使用 oauth,并已采取行动来使用像这样访问 google 用户的广告服务

公共函数 actionGoogleAds() {

    Yii::import('ext.eoauth.*');

    $ui = new EOAuthUserIdentity(
            array(
                //Set the "scope" to the service you want to use
                    'scope'=>'https://sandbox.google.com/apis/ads/publisher/',
                    'provider'=>array(
                            'request'=>'https://www.google.com/accounts/OAuthGetRequestToken',
                            'authorize'=>'https://www.google.com/accounts/OAuthAuthorizeToken',
                            'access'=>'https://www.google.com/accounts/OAuthGetAccessToken',
                    )
            )
    );

    if ($ui->authenticate()) {
        $user=Yii::app()->user;
        $user->login($ui);
        $this->redirect($user->returnUrl);
    }
    else throw new CHttpException(401, $ui->error);

}

如果我想使用其他服务,如linkedin、facebook、twitter 来注册用户,我应该只是更改范围和参数,还是必须在其他地方进行一些更改。如何将用户信息存储在自己的数据库中?

【问题讨论】:

    标签: oauth yii authentication


    【解决方案1】:

    在简单的情况下,您可以使用带有字段“*external_id*”和“provider”的表“identities”。每个 OAuth 提供者都必须提供唯一的用户标识符(仅适用于该提供者)。为了使其在您的站点上独一无二,您可以使用与提供者预定义名称(常量)配对。以及任何其他附加字段(如果提供者提供)。

    您应该在同一个表中存储内部授权的身份数据,提供者名称为“custom”(例如)。要存储密码和其他数据,请使用单独的表,该表中的 PK 将是您的“*external_id*”。通用方案。

    还有 PHP,类似这样:

    class UserIdentity extends CUserIdentity
    {
        protected $extUserID;
    
        public function __construct($extUserID)
        {
                $this->extUserID = $extUserID;
        }
      ...
        public function authenticate()
        {
          ...
          //After search $this->extUserID as PK in users table (built-in authorization)
          ...
          $identity = Identities::model()->findByAttributes(array(
                'ext_id' => $this->extUserID,
                'service' => 'forum',
             ));
          if(!count($identity))
          {
                $identity = new Identities;
                $identity->ext_id = $this->extUserID;
                $identity->service = 'forum';
                $identity->username = $userData['username'];
                $identity->save();
          }
          $this->setState('id', $identity->id);
          ...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 2010-11-26
      • 2019-11-12
      • 2017-10-12
      • 2014-12-09
      • 2012-11-27
      • 2014-04-14
      相关资源
      最近更新 更多