【问题标题】:Invalid/Expired Token error with Twitter APITwitter API 的无效/过期令牌错误
【发布时间】:2011-10-13 14:49:28
【问题描述】:

我正在使用 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();  
                      }   

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

【问题讨论】:

    标签: php twitter twitter-oauth


    【解决方案1】:

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

    This link可以让你对创建一个简单的推特应用有所了解。

    【讨论】:

    • 这是他使用的链接
    • 是的,但是链接上的代码和发布的代码在不同的地方使用会话。
    猜你喜欢
    • 2016-05-24
    • 2013-08-18
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多