Twitter API无效/过期令牌错误

时间:2011-10-13 06:11:27

标签: php twitter twitter-oauth

我正在使用Jason Mathai的PHP OAUTH库,每当我尝试使用以下测试代码访问用户的信息时,我一直收到无效/过期的令牌错误:

//sessions stuff here
 session_start(); 
//twitter stuff
include 'lib/EpiCurl.php';
include 'lib/EpiOAuth.php';
include 'lib/EpiTwitter.php';
include 'lib/secret.php';


    //ensure token is set for the session
    $twitterObj = new EpiTwitter($consumer_key, $consumer_secret); 
    if($_GET['oauth_token']){
    $_SESSION['o_token'] = $_GET['oauth_token'];
    }
    if(!isset($_SESSION['o_token']))
          { 
    $url = $twitterObj->getAuthorizationUrl();
         header("Location:$url");
            die();
                } 
    $o_token = $_SESSION['o_token'];

    $twitterObj->setToken($o_token);  
    $token = $twitterObj->getAccessToken();  
    $twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);  
    setcookie('oauth_token', $token->oauth_token);  
    setcookie('oauth_token_secret', $token->oauth_token_secret);    
    $twitterObj = new EpiTwitter($consumer_key, $consumer_secret,$_COOKIE['oauth_token'], $_COOKIE['oauth_token_secret']);  
    $twitterInfo= $twitterObj->get_accountVerify_credentials();
    $twitterInfo->response;
    var_dump($twitterInfo->response); // this tells me the error 

                try{  

                   echo $twitterInfo->screen_name;  

                    }catch(EpiTwitterException $e){  
                       echo $e->getMessage();  
                      }   

所有方法直接映射到HTTP使用twitter API调用我认为我的问题与Jason的库无关(因为它使用得相当好)但与我的逻辑有关。感谢所有帮助!

1 个答案:

答案 0 :(得分:1)

从twitter获得oauth_token后,您将其存储到会话中,并且每次使用相同的存储会话发出请求时。令牌在服务器上设置了一些到期时间,所以一段时间后,您开始获得到期令牌错误。

This link可以让您对创建简单的Twitter应用程序有所了解。

相关问题