2012-08-12 61 views
0

我一整天在執行Twitter身份驗證(使用OAuth),但我已經絕望了,仍然收到錯誤消息無法驗證您的身份。,它成爲我的噩夢。PHP Twitter OAuth - 我無法驗證我的帳戶

這裏是我的兩個文件,我使用的身份驗證:

requestOauth.php

session_start(); 
$basedir=dirname(__FILE__).""; 
include($basedir.'/config.php'); 
include_once($basedir.'/twitteroauth/twitteroauth.php'); 

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);  
$request_token = $connection->getRequestToken(OAUTH_CALLBACK); 

//$_SESSION['oauth_token'] = $token = $request_token['oauth_token']; 
//$_SESSION['oauth_token_secret'] = $secret = $request_token['oauth_token_secret']; 
$_SESSION['oauth_token'] = $request_token['oauth_token']; 
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; 

switch ($connection->http_code) { 
    case 200: 

    $url = $connection->getAuthorizeURL($_SESSION['oauth_token'], FALSE); 
    header('Location: ' . $url); 
    break; 
    default: 
    echo 'Could not connect to Twitter. Refresh the page or try again later.'; 
} 

的index.php

if(!isset($_GET['oauth_verifier'])){ 
    $link_redirect = '<a href="requestOauth.php">Login With Twitter</a>'; 
} 
else 
{ 
    if(isset($_SESSION['access_token'])){ 
     $oauth_token = $_SESSION['access_token']['oauth_token']; 
     $oauth_token_secret = $_SESSION['access_token']['oauth_token_secret']; 
     $twitterRequest = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret); 

    } else { 

     $twitterRequest = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); 
     $accessToken = $twitterRequest->getAccessToken($_GET['oauth_verifier']); 
     $_SESSION['access_token'] = $accessToken; 
     echo 'ACCESS TOKEN2: '.var_dump($_SESSION['access_token']); 
    } 

    $user_info = $twitterRequest->get('account/verify_credentials'); 
    if(isset($user_info->error)) 
    { 
     echo $user_info->error; 
     //header("location: requestOauth.php"); 
    } else { 
     var_dump($_GET); 
    } 

我仍然得到錯誤無法驗證您的身份。和已經不知道,那裏可能是什麼問題...

能否請您給我任何幫助,如何找出這個問題?

謝謝

回答

0

根據你打電話getAccessToken你從那裏得到的值之後打造的又一TwitterOAuth對象documentation, step 8

在你的情況是這樣的可以工作:

$twitterRequest = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $accessToken['oauth_token'], $accessToken['oauth_token_secret']); 
相關問題