首先,我能够成功地使用Oauth进行身份验证。我正在使用Padraic的教程,在这里找到:http://blog.astrumfutura.com/archives/411-Writing-A-Simple-Twitter-Client-Using-the-PHP-Zend-Frameworks-OAuth-Library-Zend_Oauth.html
现在,我的问题是我已经有了一个使用Zend_Service_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();的东西吗?)
我还得到了这个错误:
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发布于 2009-08-21 15:26:31
好吧,我悬赏这个问题,但我不知道如何删除它。我自己来回答。让其他人知道他们是否遇到了同样的问题。
首先,在我的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-21 18:36:13
我们已经为Twitgoo.com (运行完整zend框架和twitter登录集成)几乎完全做到了这一点。
为了概括起见,我们创建了两个Zend_Auth_Identity模型-一个用于密码登录,一个用于oauth登录。(实际上是3,其中1是'anon‘,并且失败了所有Zend_Auth)。标识至少保存用户名和本地用户it -对于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;
}
}$username需要通过一些Service_Twitter函数从登录( twitter在获取访问令牌时返回给您)设置。
如果需要的话我可以增加更多的细节。
发布于 2009-08-28 19:40:58
伙计,谢谢你这么做。我只是想在一个新项目上失去理智,这个项目已经包含了大量的Zend_Service_Twitter调用。
https://stackoverflow.com/questions/1297667
复制相似问题