dongwei7913 2014-06-25 22:09
浏览 271
已采纳

首次连接后Twitter访问资源错误

I successfully setup twitteroauth and posted a message. Now, when I try to post again, and every time AFTER THE FIRST successful message post, I get an error Your credentials do not allow access to this resource. (code 220);

Here's my code:';

    $message = 'Test';
    $apiKey = Ranger_Application_Util::getConfig('twitter_app_api_key');
    $apiSecret = Ranger_Application_Util::getConfig('twitter_app_api_secret');
    $consumerToken = Ranger_Application_Util::getStorageData('twitter_oauth_token');
    $consumerSecret = Ranger_Application_Util::getStorageData('twitter_oauth_secret');
    $twitterOauthVerifier = Ranger_Application_Util::getStorageData('twitter_oauth_verifier');      
    $_SESSIOM['oauth_token'] = $consumerToken;
    $_SESSION['oauth_token_secret'] = $consumerSecret;
    require_once 'twitter/twitteroauth.php';
    $connection = new TwitterOAuth($apiKey,$apiSecret,$consumerToken,$consumerSecret);
    $connection->host = "https://api.twitter.com/1.1/";
    $accessToken = $connection->getAccessToken($twitterOauthVerifier);
    $_SESSION['access_token'] = $accessToken;
    $parameters = array('status' => $message);
    $status = $connection->post('statuses/update',$parameters);
    Core::dump($status);
    die();

The data I retrieve from getStorageData is values stored in the database that pertain to that specific user.

  • 写回答

2条回答 默认 最新

  • douqi2571 2014-07-04 18:33
    关注

    Unless you are storing the information from $accessToken to your database in a piece of code you have not posted, I believe the issue may be that you are getting the access token after your initial connection, but not using the permanent access information from the access token when trying to post to the connection. So any future requests will not be using the permanent credentials and are therefore rejected. Try something like this:

    $message = 'Test';
    
    $apiKey               = Ranger_Application_Util::getConfig('twitter_app_api_key');
    $apiSecret            = Ranger_Application_Util::getConfig('twitter_app_api_secret');
    $twitterOauthVerifier = Ranger_Application_Util::getStorageData('twitter_oauth_verifier');  
    
    require_once 'twitter/twitteroauth.php';
    
    $hasAccessToken = (
        array_key_exists('oauth_token', $_SESSION) &&
        array_key_exists('oauth_token_secret', $_SESSION));
    
    if ($hasAccessToken)
    {
        $consumerToken  = $_SESSION['oauth_token'];
        $consumerSecret = $_SESSION['oauth_token_secret'];
    }
    else
    {
        $consumerToken  = Ranger_Application_Util::getStorageData('twitter_oauth_token');
        $consumerSecret = Ranger_Application_Util::getStorageData('twitter_oauth_secret');
    
        $connection = new TwitterOAuth($apiKey, $apiSecret, $consumerToken, $consumerSecret);
        $connection->host = "https://api.twitter.com/1.1/";
    
        $accessToken = $connection->getAccessToken($twitterOauthVerifier);
    
        $consumerToken  = $accessToken['oauth_token'];
        $consumerSecret = $accessToken['oauth_token_secret'];
        $_SESSION['oauth_token']        = $consumerToken;
        $_SESSION['oauth_token_secret'] = $consumerSecret;
    }
    
    $connection = new TwitterOAuth($apiKey, $apiSecret, $consumerToken, $consumerSecret);
    $connection->host = "https://api.twitter.com/1.1/";
    
    $parameters = array('status' => $message);
    $status = $connection->post('statuses/update', $parameters);
    
    Core::dump($status);
    die();
    

    You may want to store the oauth token information in your database after receiving it from getAccessToken() rather than using sessions. Maybe you have a method like Ranger_Application_Util::setStorageData('twitter_oauth_token', $consumerToken)?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)