Twitter API 的无效/过期令牌错误

发布于 2024-12-09 20:25:21 字数 1584 浏览 0 评论 0原文

我正在使用 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 的库无关(因为它使用得相当好),但与我的逻辑有关。感谢所有帮助!

I am using Jason Mathai's PHP OAUTH library and i keep on getting an invalid/expired token error whenever i try to access a user's information with the following test code:

//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();  
                      }   

All the methods map directly to HTTP get calls with the twitter API I assume my problem has nothing to do with Jason's library (since it is fairly well used) but something to do with my logic. All help is appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉目亦如画i 2024-12-16 20:25:21

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

此链接可以让您了解如何创建简单的 Twitter 应用程序。

Once you get a oauth_token from twitter, you are storing it into session and every time you make a request with that same stored session. Tokens have some expiry time set on server so after some time, you start getting expiry token error.

This link can give you some understanding for creating a simple twitter app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文