Zend_Service_Twitter - 让API v1.1的准备(Zend_Service_

2019-08-01 02:08发布

该Zend_Service_Twitter组件仍然是将在2013年3月5日,所以我想使我的新网站与Twitter API的交互V1.1准备弃用Twitter的API 1.0。 一切正常工作与V1.0,但如果我更改从URL /1//1.1/失败与HTTP标题代码400和JSON错误消息: Bad Authentication data (代码:215)

为了获得请求和访问令牌保持不变,并已经工作没有任何变化,但如果我想验证这样我得到我上述错误的凭据:

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

我改变了的verifyCredentials的代码Zend/Service/Twitter.php来自于:

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框架的问题跟踪bug报告: http://framework.zend.com/issues/browse/ZF-12409 (?也许做一个给予好评)

Answer 1:

与ZF 1.12.3的解决方法是通过consumerKey和consumerSecret在oauthOptions选项,在选项中不directrly。

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


Answer 2:

在你等候修复Zend_Twitter_Service组件这个问题,你可以做到这一点的解决方法:

您需要发送customerKeycustomerSecret到Zend_Service_Twitter

$twitter = new Zend_Service_Twitter(array(
                'consumerKey' => $this->consumer_key,
                'consumerSecret' => $this->consumer_secret,
                'username' => $user->screenName,
                'accessToken' => unserialize($user->token)
));


Answer 3:

今天我有同样的问题 - Zend框架的工作原理与API 1。

我创建新的类象

class Zend_Service_Twitter11 extends Zend_Service_Twitter

和覆盖功能,这是我需要的。

statusUpdate
statusReplies

等等



Answer 4:

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


文章来源: Zend_Service_Twitter - Make API v1.1 ready