2012-02-09 72 views
2

首先我想簡單地想知道如何使用oauth作品。我們需要通過這個插件以及這個插件將返回什麼。我們是否需要爲不同的php框架定製插件?我已經看到他們是不同的框架oauth的不同擴展,爲什麼?需要通過oauth插件使用Twitter,Google,Facebook Apis驗證用戶?

我需要在Yii框架使用社交網絡的用戶進行身份驗證,我已經集成警予的eouath擴展使用OAuth,並取得了使用訪問谷歌用戶的廣告服務這樣

公共職能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,微博只是註冊用戶,我應該只是改變範圍和參數,或者也不得不做出一些改變別處。如何將用戶信息存儲在我自己的數據庫中?

回答

4

在簡單的情況下,你可以使用表「身份」包含字段「* EXTERNAL_ID *」和「提供商」。每個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); 
     ... 
    } 
} 
相關問題