2009-08-19 29 views
4

首先,我能夠使用Oauth成功進行身份驗證。我使用的帕德里克的教程在這裏找到:http://blog.astrumfutura.com/archives/411-Writing-A-Simple-Twitter-Client-Using-the-PHP-Zend-Frameworks-OAuth-Library-Zend_Oauth.htmlZend Framework:Zend_Oauth和Zend_Service_Twitter

現在,我的問題是,我使用Zend_Service_Twitter已經有Twitter的模式。但是由於Zend_Service_Twitter需要密碼,我決定擴展它。我的新類是這樣的:

<?php 

/** 
* I'm trying to extend Zend_Service_Twitter to use Oauth instead 
*/ 

require_once 'Zend/Service/Twitter.php'; 

class Mytwitterapp_Twitteroauth extends Zend_Service_Twitter 
{ 
    /** 
    * Oauth 
    * @var Zend_Oauth_Token_Access 
    */ 
    protected $_token; 

    /** 
    * Array for Zend_Oauth_Consumer (i think) 
    * @var Zend_Config_Ini 
    */ 
    protected $_config; 

    /** 
    * @param object $token 
    * @return void 
    */ 
    public function __construct(Zend_Oauth_Token_Access $token) 
    { 
     $this->_config = new Zend_Config_Ini(APPLICATION_INI, APPLICATION_ENV); 

     $this->_token = $token; 
     $this->setUri('http://twitter.com'); 

     $client = $this->_token->getHttpClient($this->_config->oauth->toArray()); 
    } 

    public function _init() 
    { 
     $client = $this->_token->getHttpClient($this->_config->oauth->toArray()); 
    } 
} 

所以我Model_Twitter,看起來是這樣的:

<?php 

require_once 'Mytwitterapp/Twitteroauth.php'; 

class Twitter_Model_Twitter 
{ 
    private $_twitter; 
    private $_username; 
    private $_password; 

    public function __construct(array $options = null) 
    {   
     $oauth = new Zend_Session_Namespace('Twitter_Oauth'); 
     $token = unserialize($oauth->twitter_access_token); 
     $this->_twitter = new Mytwitterapp_Twitteroauth($token); 
    } 

    public function setOptions(array $options) 
    { 
     $methods = get_class_methods($this); 
     foreach ($options as $key => $value) { 
      $pieces = explode('_', $key); 
      foreach($pieces AS $piece_key => $piece_value) { 
       $pieces[$piece_key] = ucfirst($piece_value); 
      } 
      $name = implode('',$pieces); 
      $method = 'set' . $name; 
      if (in_array($method, $methods)) { 
       $this->$method($value); 
      } 
     } 
     return $this; 
    } 

    public function setTwitter($obj) 
    { 
     $this->_twitter = $obj; 
     return $this; 
    } 


    public function verifyCredentials() 
    { 
     return $this->_twitter->account->verifyCredentials(); 
    } 

    public function userTimeline() 
    { 
     return $this->_twitter->status->userTimeline(); 
    } 
//...more code here... 
} 

所以最後,我期待像這樣的東西用這些:

$twitter_model = new Twitter_Model_Twitter(); 
$this->view->friendsTimeline = $twitter_model->friendsTimeline(); 
  1. 我在做對吧? (關於擴展我的Zend_Service_Twitter類)。

  2. 你將如何實現這樣的事情?

我也收到此錯誤:

Zend_Rest_Client_Result_Exception: REST Response Error: fopen(/htdocs/twitter/application/views/helpers/Layout.php) [function.fopen]: failed to open stream: No such file or directory in /Applications/MAMP/bin/php5/lib/php/Zend/Rest/Client/Result.php on line 67 
+1

帖子中的鏈接已遺失。 – Ben 2010-02-15 21:24:19

回答

5

好吧,我把這個問題一賞金,我不知道如何刪除它。我將自己回答。讓其他人知道他們是否遇到同樣的問題。

首先,在我的application.ini我有這樣的事情:

oauth.version   = "1.0" 
oauth.signatureMethod = "HMAC-SHA1" 
oauth.requestScheme  = "header" 
oauth.siteUrl   = "http://mysite.com/twitter/public" 
oauth.callbackUrl  = "http://mysite.com/twitter/public/login/callback" 
oauth.requestTokenUrl = "http://twitter.com/oauth/request_token" 
oauth.authorizeUrl  = "http://twitter.com/oauth/authorize" 
oauth.accessTokenUrl = "http://twitter.com/oauth/access_token" 
oauth.consumerKey  = "xxxxxx" 
oauth.consumerSecret = "xxxxxxxxxx" 

然後,我的新的模式是這樣的:

<?php 

/** 
* I'm trying to extend Zend_Service_Twitter to use Oauth instead 
*/ 

require_once 'Zend/Service/Twitter.php'; 

class Mytwitterapp_Twitteroauth extends Zend_Service_Twitter 
{ 
    /** 
    * Oauth 
    * @var Zend_Oauth_Token_Access 
    */ 
    protected $_token; 

    /** 
    * Array for Zend_Oauth_Consumer (i think) 
    * @var Zend_Config_Ini 
    */ 
    protected $_config; 

    /** 
    * @param object $token 
    * @return void 
    */ 
    public function __construct(Zend_Oauth_Token_Access $token) 
    { 
     $this->_config = new Zend_Config_Ini(APPLICATION_INI, APPLICATION_ENV); 
     $this->_token = $token; 
     $this->setUri('http://twitter.com'); 
     //$this->_authInitialized = true; 

     self::setHttpClient($token->getHttpClient($this->_config->oauth->toArray())); 
    } 
} 

使用它會是這個樣子:

$oauth = new Zend_Session_Namespace('Twitter_Oauth'); 
$token = unserialize($oauth->twitter_access_token); 
$this->_twitter = new Mytwitterapp_Twitteroauth($token); 
3

我們已經完成了Twitgoo.com的全部工作(運行完整的zend框架和twitter oauth登錄集成)。

概括起來,我們創建了兩個Zend_Auth_Identity模型 - 一個用於oauth登錄的密碼登錄。 (實際上是3,其中1是'anon'並且全部失敗Zend_Auth)。身份至少保存用戶名和我們的本地用戶名 - 對於oauth它保存Zend_Oauth_Token,密碼保存密碼(我們永遠不會保存它)。這些身份模型是針對twitter返回的Zend_Auth_Adapter,以及我們傳遞給我們的Zend_Service_Twitter擴展的內容。

我們的twitter然後採用身份模型,並處理設置twitter。需要

class Tg_Service_Twitter extends Zend_Service_Twitter { 

    public function __construct(Tg_Auth_Identity $login) { 
     if ($login instanceof Tg_Auth_Identity_Password) { 
      $password = $login->getPassword(); 
     } else if ($login instanceof Tg_Auth_Identity_Oauth) { 
      $password = null; 
      $httpClient = $login->getOauthToken() 
       ->getHttpClient(Tg_Service_Twitter_Helper::getOauthOptions()); 
      self::setHttpClient($httpClient); 
     } else { 
      throw new Exception('not done yet'); 
     } 

     $username = $login->getUsername(); 

     self::setupHttpClient(); 
     parent::__construct($username, $password); 

     if ($login instanceof Tg_Auth_Identity_Oauth) { 
      //makes it skip basic auth 
      $this->_authInitialized = true; 
     } 
    } 

的$用戶名從登錄一些的Service_Twitter功能(獲得訪問令牌在此期間,Twitter的還給你)設置。

如果需要,我可以添加更多細節。

+0

是的,我記得Padriac的帖子中的Twitgoo。我現在可以使用Zend_Oauth來獲取/發佈到twitter.com。我的問題是,我得到這個錯誤:開捕致命錯誤:參數2,以Zend_Oauth_Token_Access :: toQueryString()必須實現接口Zend_Oauth_Config_Interface,Zend_Oauth_Client的情況下給出的,稱爲/htdocs/twitter/library/Zend/Oauth/Client.php上線131和/htdocs/twitter/library/Zend/Oauth/Token/Access.php定義上線55 當你開發Twitgoo你有沒有遇到過這個錯誤? – wenbert 2009-08-22 12:32:50

+0

我懂了! Padraic Brady在我的ZF郵件列表中回覆了我的帖子......這實際上是一個錯誤。我剛剛從孵化器更新了Oauth,問題就消失了。 – wenbert 2009-08-22 13:06:53

+0

嗯,我忘了提及那個:-) - 在孵化器更新了修復程序之前,我有一個臨時補丁。 – Justin 2009-08-23 01:49:43

0

男人,謝謝你。只是在一個已經有一堆Zend_Service_Twitter調用的新項目中失去了我的想法。