2011-08-25 35 views
0

編輯: 我很抱歉,因爲我想出了這個問題,使這個線程。亞伯拉罕威廉姆斯Twitter的OAuth 303錯誤

以下是發生了什麼情況: 首先,我注意到發送的URL在.com和account/verify_credentials之間缺少「/」。當我更新時,我得到了404錯誤。然後我看着twitteroauth.php,我注意到頂部的一個:

/* Set up the API root URL. */ 
public $host = "https://api.twitter.com/1/"; 
// public $host = "https://dev.twitter.com"; 

被註釋掉了。我取消了評論,並且評論了較低的那個(我猜測它並沒有停用或不被使用?),現在它工作得很好!

謝謝大家,並感謝威廉姆斯先生爲一個偉大的奧尤斯圖書館!


我已經實現亞伯拉罕·威廉姆斯Twitter的OAuth的圖書館,它的進展順利,到目前爲止,直到我試圖連接到Twitter後,用它做什麼。那是當我得到一個303 HTTP代碼。然後,我改變了我的實現方式,以便他在他的github網站上實現它,並且我仍然遇到同樣的問題。有沒有人知道發生了什麼?

apitest.php是問題發生的地方,其中$ connection變量有一個303 HTTP代碼。 redirect.php和callback.php中的相應$連接變量都返回200.順便說一句,所有文件都在同一個目錄中。

下面的代碼:

connect.php(此頁面上的HTML有一個按鈕,進入redirect.php)

<?php 
/** 
* @file 
* Check if consumer token is set and if so send user to get a request token. 
*/ 

/** 
* Exit with an error message if the CONSUMER_KEY or CONSUMER_SECRET is not defined. 
*/ 
require_once('twitconfig.php'); 
if (CONSUMER_KEY === '' || CONSUMER_SECRET === '') { 
    echo 'A consumer key and secret are required.'; 
    exit(); 
} 

?> 

redirect.php

<?php 

/* Start session and load library. */ 
session_start(); 
require_once('twitteroauth.php'); 
require_once('twitconfig.php'); 

/* Build TwitterOAuth object with client credentials. */ 
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET); 

/* Get temporary credentials. */ 
$request_token = $connection->getRequestToken(OAUTH_CALLBACK); 

/* Save temporary credentials to session. */ 
$_SESSION['oauth_token'] = $token = $request_token['oauth_token']; 
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; 

/* If last connection failed don't display authorization link. */ 
switch ($connection->http_code) { 
    case 200: 
    /* Build authorize URL and redirect user to Twitter. */ 
    $url = $connection->getAuthorizeURL($token); 
    header('Location: ' . $url); 
    break; 
    default: 
    /* Show notification if something went wrong. */ 
    echo 'Could not connect to Twitter. Refresh the page or try again later.'; 
} 

?> 

回調。 php

<?php 

/** 
* @file 
* Take the user when they return from Twitter. Get access tokens. 
* Verify credentials and redirect to based on response from Twitter. 
*/ 

/* Start session and load lib */ 
session_start(); 
require_once('twitteroauth.php'); 
require_once('twitconfig.php'); 

/* If the oauth_token is old redirect to the connect page. */ 
if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) { 
    $_SESSION['oauth_status'] = 'oldtoken'; 
    header('Location: ./clearsessions.php'); 
} 

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

/* Request access tokens from twitter */ 
$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']); 

/* Save the access tokens. Normally these would be saved in a database for future use. */ 
$_SESSION['access_token'] = $access_token; 

/* Remove no longer needed request tokens */ 
unset($_SESSION['oauth_token']); 
unset($_SESSION['oauth_token_secret']); 


/* If HTTP response is 200 continue otherwise send to connect page to retry */ 
if (200 == $connection->http_code) { 
    /* The user has been verified and the access tokens can be saved for future use */ 
    $_SESSION['status'] = 'verified'; 
    header('Location: ./apitest.php'); 
} else { 
    /* Save HTTP status for error dialog on connnect page.*/ 
    header('Location: ./clearsessions.php'); 
} 

?> 

apitest.php(那裏的問題通過的print_r所見)

<?php 

session_start(); 
require_once('twitteroauth.php'); 
require_once('twitconfig.php'); 

if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) { 
    header('Location: ./clearsessions.php'); 
} 

/* Get user access tokens out of the session. */ 
$access_token = $_SESSION['access_token']; 

/* Create a TwitterOauth object with consumer/user tokens. */ 
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']); 


/* Get logged in user to help with tests. */ 
$user = $connection->get('account/verify_credentials'); 

print_r($connection); 

print_r($_SESSION); 

?> 

第一打印語句的輸出是:

TwitterOAuth Object ( 
[http_code] => 303 
[url] => https://dev.twitter.comaccount/verify_credentials.json?oauth_consumer_key=UoXXKdfnVHh2fYD8Csw&oauth_nonce=09bc369393a8eab3a1dc36eaa6230b45&oauth_signature=pe5dYtYq%2FvlGb0c%2BnRR%2BQAXI0Ec%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1314249851&oauth_token=351699791-F6stFFk81poF8ooORgLLxaE2cpSdDT2Vr0Tz9yJm&oauth_version=1.0 
[host] => https://dev.twitter.com 
[timeout] => 30 
[connecttimeout] => 30 
[ssl_verifypeer] => 
[format] => json 
[decode_json] => 1 
[http_info] => Array ([url] => https://dev.twitter.comaccount/verify_credentials.json?oauth_consumer_key=UoXXKdfnVHh2fYD8Csw&oauth_nonce=09bc369393a8eab3a1dc36eaa6230b45&oauth_signature=pe5dYtYq%2FvlGb0c%2BnRR%2BQAXI0Ec%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1314249851&oauth_token=351699791-F6stFFk81poF8ooORgLLxaE2cpSdDT2Vr0Tz9yJm&oauth_version=1.0 
[content_type] => 
[http_code] => 303 
[header_size] => 565 
[request_size] => 401 
[filetime] => -1 
[ssl_verify_result] => 1 
[redirect_count] => 0 
[total_time] => 0.053773 
[namelookup_time] => 0.003355 
[connect_time] => 0.006399 
[pretransfer_time] => 0.047636 
[size_upload] => 0 
[size_download] => 0 
[speed_download] => 0 
[speed_upload] => 0 
[download_content_length] => 0 
[upload_content_length] => 0 
[starttransfer_time] => 0.053629 
[redirect_time] => 0 
[certinfo] => Array ()) 
[useragent] => TwitterOAuth v0.2.0-beta2 
[sha1_method] => OAuthSignatureMethod_HMAC_SHA1 Object () 
[consumer] => OAuthConsumer Object ([key] => UoXXKdfnVHh2fYD8Csw [secret] => SyKfxRorvUs6THwj5l26TGEkFOCGf1vG86N7PoS97o [callback_url] =>) [token] => OAuthConsumer Object ([key] => 351699791-F6stFFk81poF8ooORgLLxaE2cpSdDT2Vr0Tz9yJm [secret] => q8fHaTaFvOUL8XKYX89LSgubr2fSl1xHBUAi8MUqIc [callback_url] =>) 

[http_header] => Array ([location] => http://guide.a.id.opendns.com/?url=dev%2Etwitter%2Ecomaccount%2Fverify%5Fcredentials%2Ejson%3Foauth%5Fconsumer%5Fkey%3DUoXXKdfnVHh2fYD8Csw%26oauth%5Fnonce%3D09bc369393a8eab3a1dc36eaa6230b45%26oauth%5Fsignature%3Dpe5dYtYq%252FvlGb0c%252BnRR%252BQAXI0Ec%253D%26oauth%5Fsignature%5Fmethod%3DHMAC%2DSHA1%26oauth%5Ftimestamp%3D1314249851%26oauth%5Ftoken%3D351699791%2DF6stFFk81poF8ooORgLLxaE2cpSdDT2Vr0Tz9yJm%26oauth%5Fversion%3D1%2E0 
[content_length] => 0 
[connection] => close 
[date] => Thu, 25 Aug 2011 05:24:50 GMT 
[server] => OpenDNS Guide)) 

第二打印語句的輸出是:

Array ( 
[access_token] => Array ( 
[oauth_token] => 351699791-F6stFFk81poF8ooORgLLxaE2cpSdDT2Vr0Tz9yJm 
[oauth_token_secret] => q8fHaTaFvOUL8XKYX89LSgubr2fSl1xHBUAi8MUqIc 
[user_id] => 351699791 
[screen_name] => jibjib21) 
[status] => verified) 

有沒有人有這個303錯誤怎麼回事以及如何解決它的想法?任何幫助將不勝感激。謝謝!

回答

0

303響應是重定向。我的猜測是你需要將.json(或者你正在使用的任何格式)添加到你請求的URL的末尾。

I.e.而不是'account/verify_credentials',你需要申請'account/verify_credentials.json'。

+0

如果您查看第一個打印語句輸出中的[url]部分,則.json已經存在。爲了好的措施,我嘗試了你所做的,並且在第一個之後附加了另一個.json,並且仍然給出了相同的303代碼。不過,感謝您的考慮。 – praetor

相關問題