2

我正在尝试使用 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

4

1 回答 1

5

如果您只想显示自己的时间线,您可以在 Twitter 开发人员页面上“直接”请求令牌,并将其“硬编码”到您的应用程序中。所以你不必处理重定向和身份验证过程。试试这个:(未经测试,因为我正在运行..)

    $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();
于 2012-09-07T05:29:41.623 回答