2015-02-21 28 views
5

Próbuję udostępnić wpis za pomocą twitteroauth z najnowszą wersją. Poniżej znajduje się mój kod.OAuth na Twitterze - Nieprawidłowy lub wygasły token dostępu

require APP."Vendor/twitteroauth-master/autoload.php"; // twitter v0.5.1 SDK 

if(isset($_REQUEST['oauth_verifier'])) { 

     $request_token = []; 
     $request_token['oauth_token'] = $this->Session->read('twtr_oauth_token'); 
     $request_token['oauth_token_secret'] = $this->Session->read('twtr_oauth_token_secret'); 

     /* If denied, bail. */ 
     if (isset($_REQUEST['denied'])) { 
      exit('Permission was denied. Please start over.'); 
     } 

     /* If the oauth_token is not what we expect, bail. */ 
     if (isset($_REQUEST['oauth_token']) && $request_token['oauth_token'] !== $_REQUEST['oauth_token']) { 
      $this->Session->write("twtr_oauth_token" , ''); 
      $this->Session->write("twtr_oauth_token_secret" , ''); 
      exit; 
     } 

     debug($request_token); 

     /* Create TwitteroAuth object with app key/secret and token key/secret from default phase */ 
     $connection = new TwitterOAuth("KEY", "SECRET", $request_token['oauth_token'], $request_token['oauth_token_secret']); 

     debug($connection); 

     $content = $connection->get('account/verify_credentials'); 

     debug($content); 

     /* Request access token from twitter */ 
     $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier'])); 

     /** Finally post things */ 
     $result = $connection->post("statuses/update", array("status" => "hello world")); 

     if(isset($result->errors) && count($result->errors) > 0) { 
      debug($connection->post("statuses/update", array("status" => "hello world"))); 
      exit; 
     } 

     $this->redirect("http://twitter.com"); 

    } else { 

     $connection = new TwitterOAuth("KEY", "SECRET"); 

     /** Get Temp Token */ 
     $request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => "https://180.211.99.162:9051/widgetapi/twitterOauth")); 

     switch ($connection->getLastHttpCode()) { 
      case 200: 
       /** Write OAuth token and secret into session */ 
       $this->Session->write("twtr_oauth_token" , $request_token['oauth_token']); 
       $this->Session->write("twtr_oauth_token_secret" , $request_token['oauth_token_secret']); 

       /** Build Auth URL */ 
       $url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token'])); 

       /** Redirect */ 
       $this->redirect($url); 
       break; 
      default: 
       /* Show notification if something went wrong. */ 
       echo 'Could not connect to Twitter. Refresh the page or try again later.'; 
     } 
    } 

Kiedy próbuję to osiągnąć, otrzymuję nieważny lub wygasły token dostępu.

informacje debugowania

/app/Controller/WidgetapiController.php (line 258) 
array(
    'oauth_token' => 'AU549WSBbnxvibRcU5jzUzOwFAoUiTXs', 
    'oauth_token_secret' => 'NRmkbByjjAPGQLJK06DaUauYfa38kpY6' 
) 
/app/Controller/WidgetapiController.php (line 261) 
object(Abraham\TwitterOAuth\TwitterOAuth) { 
    [protected] timeout => (int) 5 
    [protected] connectionTimeout => (int) 5 
    [protected] decodeJsonAsArray => false 
    [protected] userAgent => 'TwitterOAuth (+https://twitteroauth.com)' 
    [protected] proxy => array() 
    [private] response => object(Abraham\TwitterOAuth\Response) { 
     [private] apiPath => null 
     [private] httpCode => (int) 0 
     [private] headers => array() 
     [private] body => array() 
     [private] xHeaders => array() 
    } 
    [private] bearer => null 
    [private] consumer => object(Abraham\TwitterOAuth\Consumer) { 
     key => 'KEY' 
     secret => 'SECRET' 
     callbackUrl => null 
    } 
    [private] token => object(Abraham\TwitterOAuth\Token) { 
     key => 'AU549WSBbnxvibRcU5jzUzOwFAoUiTXs' 
     secret => 'NRmkbByjjAPGQLJK06DaUauYfa38kpY6' 
    } 
    [private] signatureMethod => object(Abraham\TwitterOAuth\HmacSha1) { 

    } 
} 
/app/Controller/WidgetapiController.php (line 263) 
object(stdClass) { 
    errors => array(
     (int) 0 => object(stdClass) { 
      code => (int) 89 
      message => 'Invalid or expired token.' 
     } 
    ) 
} 
/app/Controller/WidgetapiController.php (line 271) 
object(stdClass) { 
    errors => array(
     (int) 0 => object(stdClass) { 
      code => (int) 89 
      message => 'Invalid or expired token.' 
     } 
    ) 
} 

nie jestem w stanie dowiedzieć się tego problemu! Masz pojęcie, co jest nie tak?

Odpowiedz

13

Gdy masz oddzwonienia, ponownie klasy initialize z nowym token dostępu

/* Request access token from twitter */ 
$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier'])); 

$connection = new TwitterOAuth("KEY", "SECRET", $access_token['oauth_token'], $access_token['oauth_token_secret']); 
+0

pozwól mi spróbować tego. – Shreejibawa

+0

który działał! wielkie dzięki! :) – Shreejibawa

+0

dla mnie dałem też oauth_token. aby działał w linii $ access_token = $ connection-> oauth ("oauth/access_token", array ("oauth_verifier" => $ _REQUEST ['oauth_verifier'], "oauth_token" => $ oauth_token)); – zoomi

4

wykorzystaniem najnowszej wersji TwitterOAuth oraz z pomocą użytkownika @ user3989103 odpowiedź w tym wątku i https://twittercommunity.com/t/twitter-oauth-verifier-does-not-always-work/31672, co pracował dla mnie :

$connection = new TwitterOAuth($key, $secret, $_SESSION['request_token'], $_SESSION['request_token_secret']); 

$access_token = $connection->oauth('oauth/access_token', array('oauth_verifier' => $_REQUEST['oauth_verifier'], 'oauth_token'=> $_REQUEST['oauth_token'])); 

$connection = new TwitterOAuth($key, $secret, $access_token['oauth_token'], $access_token['oauth_token_secret']); 

$credentials = $connection->get('account/verify_credentials');