Zend_Service_Twitter组件仍然适用于Twitters API v1.0,将于2013年3月5日弃用。因此,我希望将我的新网站与Twitter API交互v1.1准备好。
使用v1.0时一切正常,但是如果我将URL从/1/
更改为/1.1/
,则会因HTTP标头代码400和JSON错误消息而失败:Bad Authentication data
(代码:215)< / p>
要获取请求和访问令牌保持不变并且已经运行而没有任何更改, 但如果我想验证这样的凭据,我会得到上面描述的错误:
// 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());
我将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());
}
现在我在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"
正如您所见,oauth_consumer_key
(以及realm
也是空的)。这可能是错误吗?我怎么能解决这个错误(因为更新的API版本更严格?)?以某种方式设置oauth_consumer_key
可以吗?如果是的话,我该如何管理?
编辑: 我还发现了一个关于Zend Framework问题跟踪器的错误报告: http://framework.zend.com/issues/browse/ZF-12409(也许做一个upvote?)
答案 0 :(得分:9)
使用ZF 1.12.3,解决方法是在oauthOptions选项中传递consumerKey和consumerSecret,而不是直接在选项中传递。
$options = array(
'username' => /*...*/,
'accessToken' => /*...*/,
'oauthOptions' => array(
'consumerKey' => /*...*/,
'consumerSecret' => /*...*/,
)
);
答案 1 :(得分:1)
在等待在Zend_Twitter_Service组件中修复此问题时,您可以执行此解决方法:
您需要将 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)
));
答案 2 :(得分:0)
今天我遇到了同样的问题 - Zend Framework与API 1一起使用。
我创建了像
这样的新类class Zend_Service_Twitter11 extends Zend_Service_Twitter
并覆盖我需要的功能。
statusUpdate
statusReplies
等
答案 3 :(得分:0)
$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()