我正在尝试使用 Zend 开发一个模块,该模块将查找我的最新推文并显示在我的博客上。
我已经阅读并了解了 Zend_Service_Twitter
,通过这门课,我想我应该从我的 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
有何共同点?
最佳答案
如果您只想显示您自己的时间线,您可以在 Twitter 开发者页面上“直接”请求 token ,并将其“硬编码”到您的应用程序中。因此您不必处理重定向和身份验证过程。试试这个:(未经测试,因为我正在运行..)
$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();
关于php - Zend_Service_Twitter - 在尝试任何 API 之前使用其 OAuth 功能获取访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12309661/