开发者

Invalid/Expired Token error with Twitter API

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 开发者_StackOverflowI 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜