首次连接后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.

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)?

This

$_SESSIOM['oauth_token'] = $consumerToken;
        ^

should be

$_SESSION['oauth_token'] = $consumerToken;