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 QQ邮箱过期怎么恢复?
  • ¥15 登录他人的vue项目显示服务器错误
  • ¥15 (标签-android|关键词-app)
  • ¥60 如何批量获取json的url
  • ¥15 comsol仿真压阻传感器
  • ¥15 Python线性规划函数optimize.linprog求解为整数
  • ¥15 llama3中文版微调
  • ¥15 pg数据库导入数据序列重复
  • ¥15 三分类机器学习模型可视化分析
  • ¥15 本地测试网站127.0.0.1 已拒绝连接,如何解决?(标签-ubuntu)