Zend_Service_Twitter - 在尝试任何API之前,使用其OAuth功能获取访问令牌

时间:2012-09-06 23:03:34

标签: php zend-framework twitter twitter-oauth

我正在尝试使用Zend开发一个模块,该模块将查找我的最新推文并在我的博客上显示。

我已经阅读并了解了Zend_Service_Twitter,在本课程中,我想我应该从我的推文中获取最新的推文。

当我启动下面给出的代码时:

public function indexAction() {

        $token = unserialize('XXXXX');      
        $twitter = new Zend_Service_Twitter(array(
             'username' => 'XXX',
             'accessToken' => $token,
             'consumerKey' => 'XXXX',
             'consumerSecret' => 'XXXX',
             'callbackUrl' => 'http://localhost/zendtest/public/blog'
         ));            

        // verify user's credentials with Twitter
        $response = $twitter->account->verifyCredentials();

        // Get Timeline
        $response = $twitter->status->userTimeline();

        $this->view->twitresponse = $response;

    }

我收到了这个错误:

Fatal error:
 Uncaught exception 'Zend_Service_Twitter_Exception' with message 'Twitter session is unauthorised. You need to initialize Zend_Service_Twitter with an OAuth Access Token or use its OAuth functionality to obtain an Access Token before attempting any API actions that require authorisation' in D:\Server\xampp\htdocs\zendtest\library\Zend\Service\Twitter.php:282

Stack trace: 
 #0 D:\Server\xampp\htdocs\zendtest\library\Zend\Service\Twitter.php(769): Zend_Service_Twitter->_init() 
 #1 [internal function]: Zend_Service_Twitter->accountVerifyCredentials()
 #2 D:\Server\xampp\htdocs\zendtest\library\Zend\Service\Twitter.php(270): call_user_func_array(Array, Array)
 #3 [internal function]: Zend_Service_Twitter->__call('verifyCredentia...', Array) 
 #4 D:\Server\xampp\htdocs\zendtest\application\modules\twitter\controllers\TwitterServiceController.php(27): Zend_Service_Twitter->verifyCredentials() 
 #5 D:\Server\xampp\htdocs\zendtest\library\Zend\Controller\Action.php(516): Twitter_TwitterServiceController->indexAction() 
 #6 D:\S in D:\Server\xampp\htdocs\zendtest\library\Zend\Controller\Plugin\Broker.php on line 336

我在互联网上有很多文章,但仍然无法解决我的问题。

有谁知道如何处理这个问题? 与Zend_Oauth_Consumer有什么共同之处?

1 个答案:

答案 0 :(得分:5)

如果您只想显示自己的时间表,可以在Twitter Developer页面上“直接”请求令牌,并将其“硬编码”到您的应用程序中。因此,您不必处理重定向和身份验证过程。试试这个:(未经测试,因为iam在奔跑......)

    $token = new Zend_Oauth_Token_Access();

    $token->setParams(array(
        'oauth_token'        => 'oAUTH-TOKEN',
        'oauth_token_secret' => 'oAUTH-TOKEN-SECRET',
    ));

    $twitter = new Zend_Service_Twitter(array(
        'username'       => 'USER',
        'consumerKey'    => 'CONSUMER-KEY',
        'consumerSecret' => 'CONSUMER-SECRET',
        'accessToken'    => $token
    ));


    $timeLine = $twitter->status->userTimeline();