2011-10-13 39 views
1

我使用賈森馬的PHP OAUTH庫令牌錯誤,我不斷收到一個無效/到期令牌錯誤,每當我嘗試用下面的測試代碼訪問用戶的信息:無效/過期與Twitter API

//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

一旦您從twitter獲得oauth_token,就會將其存儲到會話中,並且每次您使用同一個存儲會話發出請求。令牌在服務器上設置了一些到期時間,因此一段時間後,您會開始獲取到期令牌錯誤。

This link可以爲您創建一個簡單的Twitter應用程序的一些理解。

+1

這是他使用的鏈接 – tekknolagi

+0

我認爲。至少從代碼。 – tekknolagi

+0

雅,但鏈接和發佈代碼上的代碼正在不同的地方使用會話。 –

相關問題