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

时间:2014-06-25 22:09:29

标签: php twitter oauth twitter-oauth

我成功设置了twitteroauth并发布了一条消息。现在,当我尝试再次发帖时,每次AFTER THE FIRST成功发送消息时,都会收到错误Your credentials do not allow access to this resource. (code 220);

这是我的代码:';

    $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();

我从getStorageData检索的数据是存储在数据库中的与该特定用户相关的值。

2 个答案:

答案 0 :(得分:1)

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

应该是

$_SESSION['oauth_token'] = $consumerToken;

答案 1 :(得分:1)

除非您将$ accessToken中的信息存储在您尚未发布的一段代码中,否则我认为问题可能是您在初始连接后获取访问令牌,但未使用永久访问信息尝试发布到连接时从访问令牌。因此,任何未来的请求都不会使用永久凭证,因此会被拒绝。尝试这样的事情:

$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();

您可能希望在从getAccessToken()收到oauth令牌信息而不是使用会话后将其存储在数据库中。也许你有一个类似Ranger_Application_Util::setStorageData('twitter_oauth_token', $consumerToken)的方法?