[英]Zend_Service_Twitter - Make API v1.1 ready
The Zend_Service_Twitter component is still for Twitters API v1.0 which will be deprecated at 5th March 2013. So I wanted to make my new website with Twitter API interaction v1.1 ready. Zend_Service_Twitter组件仍然适用于Twitters API v1.0,将于2013年3月5日弃用。因此,我希望使用Twitter API交互v1.1准备好我的新网站。 Everything works fine with v1.0 but if I change the URL from
/1/
to /1.1/
it fails with the HTTP header code 400 and the JSON error message: Bad Authentication data
(Code: 215) 使用v1.0时一切正常,但如果我将URL从
/1/
更改为/1.1/
则会因HTTP标头代码400和JSON错误消息而失败: Bad Authentication data
(代码:215)
To get the request and access token stayed the same and works already without any changes, but if I want to verify the credentials like this I get the error I described above: 为了获得请求和访问令牌保持不变并且已经没有任何更改,但是如果我想验证这样的凭据,我得到上面描述的错误:
// Take a look for the code here: http://framework.zend.com/manual/1.12/en/zend.oauth.introduction.html
$accessToken = $twitterAuth->getAccessToken($_GET, unserialize($_SESSION['TWITTER_REQUEST_TOKEN']));
// I have a valid access token and now the problematic part
$twitter = new Zend_Service_Twitter(array(
'username' => $accessToken->getParam('screen_name'),
'accessToken' => $accessToken
));
print_r($twitter->account->verifyCredentials());
I changed the code of verifyCredentials in Zend/Service/Twitter.php
from that to that: 我将
Zend/Service/Twitter.php
的verifyCredentials代码更改为:
public function accountVerifyCredentials()
{
$this->_init();
$response = $this->_get('/1/account/verify_credentials.xml');
return new Zend_Rest_Client_Result($response->getBody());
}
// to
public function accountVerifyCredentials()
{
$this->_init();
$response = $this->_get('/1.1/account/verify_credentials.json');
return Zend_Json::decode($response->getBody());
}
Now I added before the return Zend_Json[...]
this line: 现在我在
return Zend_Json[...]
之前添加了这一行:
print_r($this->_localHttpClient->getLastRequest());
// And I get this output of it:
GET /1.1/account/verify_credentials.json HTTP/1.1
Host: api.twitter.com
Connection: close
Accept-encoding: gzip, deflate
User-Agent: Zend_Http_Client
Accept-Charset: ISO-8859-1,utf-8
Authorization: OAuth realm="",oauth_consumer_key="",oauth_nonce="91b6160db351060cdf4c774c78e2d0f2",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1349107209",oauth_version="1.0",oauth_token="hereismytoken",oauth_signature="hereisavalidsignature"
As you could see the oauth_consumer_key
(and realm
too) is empty. 正如您所看到的,
oauth_consumer_key
(以及realm
)也是空的。 Could that be the error? 这可能是错误吗? How could I solve this error (because of the stricter new API version?)?
我怎么能解决这个错误(因为更新的API版本更严格?)? Would it be fine to set somehow the
oauth_consumer_key
? 以某种方式设置
oauth_consumer_key
吗? If yes, how could I manage that? 如果是的话,我怎么能管理呢?
Edit: I also found already a bug report on the issue tracker of the Zend Framework: http://framework.zend.com/issues/browse/ZF-12409 (maybe do an upvote?) 编辑:我还发现了一个关于Zend框架问题跟踪器的错误报告: http : //framework.zend.com/issues/browse/ZF-12409 (也许做一个upvote?)
with ZF 1.12.3 the workaround is to pass consumerKey and consumerSecret in oauthOptions option, not directrly in the options. 使用ZF 1.12.3,解决方法是在oauthOptions选项中传递consumerKey和consumerSecret,而不是直接在选项中传递。
$options = array(
'username' => /*...*/,
'accessToken' => /*...*/,
'oauthOptions' => array(
'consumerKey' => /*...*/,
'consumerSecret' => /*...*/,
)
);
While you wait to fix this issue in Zend_Twitter_Service component, you can do this workaround: 在等待在Zend_Twitter_Service组件中解决此问题时,您可以执行以下操作:
You need to send customerKey and customerSecret to Zend_Service_Twitter 您需要将customerKey和customerSecret发送到Zend_Service_Twitter
$twitter = new Zend_Service_Twitter(array(
'consumerKey' => $this->consumer_key,
'consumerSecret' => $this->consumer_secret,
'username' => $user->screenName,
'accessToken' => unserialize($user->token)
));
Today I have the same problem - Zend Framework works with API 1. 今天我遇到了同样的问题 - Zend Framework与API 1一起使用。
I created new class like 我创建了新类
class Zend_Service_Twitter11 extends Zend_Service_Twitter
And override functions, which I need. 并覆盖我需要的功能。
statusUpdate
statusReplies
etc 等等
$this->_session = new Zend_Session_Namespace('auth_twitter');
$config = Zend_Registry::get('config')->twitter->toArray();
$access_tokenSession = unserialize($this->_session->access_token);
$accessToken = new Zend_Oauth_Token_Access();
$accessToken->setToken($access_tokenSession->oauth_token);
$accessToken->setTokenSecret($access_tokenSession->oauth_token_secret);
$temp = array();
$temp['oauthOptions']['consumerKey'] = $config['consumerKey'];
$temp['oauthOptions']['consumerSecret'] = $config['consumerSecret'];
$temp['accessToken'] = $accessToken;
$temp['username'] = $access_tokenSession->screen_name;
$this->_twitter = new Zend_Service_Twitter($temp, null);
$this->_twitter->account->accountVerifyCredentials()->toValue()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.