Zend_Service_Twitter - 准备好 API v1.1 [英] Zend_Service_Twitter - Make API v1.1 ready

查看:16
本文介绍了Zend_Service_Twitter - 准备好 API v1.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Zend_Service_Twitter 组件仍然用于 Twitter API v1.0,它将在 2013 年 3 月 5 日弃用.所以我想让我的新网站与 Twitter API 交互 v1.1 做好准备.v1.0 一切正常,但如果我将 URL 从 /1/ 更改为 /1.1/,它会失败并显示 HTTP 标头代码 400 和 JSON 错误消息:Bad Authentication data (Code: 215)

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. 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)

为了让请求和访问令牌保持不变并且已经可以正常工作而无需任何更改,但是如果我想像这样验证凭据,我会收到上面描述的错误:

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());

我将Zend/Service/Twitter.php中verifyCredentials的代码从那个改为那个:

I changed the code of verifyCredentials in Zend/Service/Twitter.php from that to that:

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[...] 之前添加了这一行:

Now I added before the return Zend_Json[...] this line:

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 会好吗?如果是,我该如何管理?

As you could see the oauth_consumer_key (and realm too) is empty. Could that be the error? How could I solve this error (because of the stricter new API version?)? Would it be fine to set somehow the oauth_consumer_key? If yes, how could I manage that?

我还发现了关于 Zend Framework 的问题跟踪器的错误报告:http://framework.zend.com/issues/browse/ZF-12409(也许会点赞?)

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?)

推荐答案

对于 ZF 1.12.3,解决方法是在 oauthOptions 选项中传递 consumerKey 和 consumerSecret,而不是直接在选项中传递.

with ZF 1.12.3 the workaround is to pass consumerKey and consumerSecret in oauthOptions option, not directrly in the options.

        $options = array(
            'username' => /*...*/,
            'accessToken' => /*...*/,
            'oauthOptions' => array(
                'consumerKey' => /*...*/,
                'consumerSecret' => /*...*/,
            )
        );

这篇关于Zend_Service_Twitter - 准备好 API v1.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆