4

まず、Oauth を使用して正常に認証できます。ここにある Padraic のチュートリアルを使用しています

さて、私の問題は、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
4

3 に答える 3

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);
于 2009-08-21T15:26:31.973 に答える
3

私たちは、Twitgoo.com (完全な zend フレームワークと twitter oauth ログイン統合を実行) に対してほぼ正確にこれを行いました。

一般化するために、2 つのモデルを作成しました。1Zend_Auth_Identityつはパスワード ログイン用で、もう 1 つは oauth ログイン用です。(実際には 3 で、1 は「anon」であり、すべて失敗しますZend_Auth)。ID には、少なくともユーザー名とローカル ユーザー ID が保持されます。oauth の場合は が保持Zend_Oauth_Tokenされ、パスワードの場合はパスワードが保持されます (保存されることはありません)。これらの ID モデルは、Zend_Auth_Adapterfor twitter が返すものであり、Zend_Service_Twitter拡張機能に渡すものです。

次に、Twitter は Identity モデルを受け取り、そのための 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 から返される) から $username を設定する必要があります。

必要に応じて詳細を追加できます。

于 2009-08-21T18:36:13.603 に答える
0

男、これに感謝します。すでにたくさんのZend_Service_Twitter電話がかかってきた新しいプロジェクトで頭がおかしくなりそうでした。

于 2009-08-28T19:40:58.057 に答える