2013-06-28 69 views
2

我使用了一個修改過的php腳本,我在互聯網上發現它可以作爲javascript和twitter api 1.1之間的代理(我需要這樣做,因爲javascript不能做oauth操作和api 1.1要求完全一樣:認證)。Twitter api 1.1 oauth在搜索#標籤時失敗

該腳本工作正常,直到我搜索一個hashtag,然後oauth失敗。

這裏的curl_info的一個例子@ z25org

url: http://api.twitter.com/1.1/search/tweets.json?q=%40z25org 
content_type: application/json;charset=utf-8 
http_code: 200 

正如你所看到的這個工程(HTTP_CODE:200)搜索時,我從Twitter獲得。但是,當我搜索標籤:

url: http://api.twitter.com/1.1/search/tweets.json?q=%23z25org 
content_type: application/json; charset=utf-8 
http_code: 401 

我得到http_code 401:未經授權的訪問。 JSON的:

{"errors":[{"message":"Could not authenticate you","code":32}]} 

這裏是我的PHP代碼:(當然,它的最大的部分)

<?php 
// Some characters that need to be replaced 
$specialCharacters = array(
    "@"=>"%40", 
    "#"=>"%23", 
    " "=>"%20", 
    ""=>"" 
); 

/* 
* Ok, no more config should really be needed. Yay! 
*/ 

// We'll get the URL from $_GET[]. Make sure the url is url encoded, for example encodeURIComponent('statuses/user_timeline.json?screen_name=MikeRogers0&count=10&include_rts=false&exclude_replies=true') 
if(!isset($_GET['url'])){ 
    die('No URL set'); 
} 

$url = $_GET['url']; 

// Figure out the URL parmaters 
$url_parts = parse_url($url); 
parse_str($url_parts['query'], $url_arguments); 

$full_url = $config['base_url'].$url; // Url with the query on it. 
$base_url = $config['base_url'].$url_parts['path']; // Url without the query. 

if (!dbglog(" > ORIGINAL: ".$full_url)) { die("Huh?"); } 

// Replace characters 
foreach($specialCharacters as $lookup => $replace) { 
    $full_url = str_replace($lookup,$replace,$full_url); 
} 

if (!dbglog(" > REPLACED: ".$full_url)) { die("Huh?"); } 

/** 
* Code below from http://stackoverflow.com/questions/12916539/simplest-php-example-retrieving-user-timeline-with-twitter-api-version-1-1 by Rivers 
* with a few modfications by Mike Rogers to support variables in the URL nicely 
*/ 

function buildBaseString($baseURI, $method, $params) { 
    $r = array(); 
    ksort($params); 
    foreach($params as $key=>$value){ 
    $r[] = "$key=" . rawurlencode($value); 
    } 
    return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r)); 
} 

function buildAuthorizationHeader($oauth) { 
    $r = 'Authorization: OAuth '; 
    $values = array(); 
    foreach($oauth as $key=>$value) 
    $values[] = "$key=\"" . rawurlencode($value) . "\""; 
    $r .= implode(', ', $values); 
    return $r; 
} 

// Set up the oauth Authorization array 
$oauth = array(
    'oauth_consumer_key' => $config['consumer_key'], 
    'oauth_nonce' => time(), 
    'oauth_signature_method' => 'HMAC-SHA1', 
    'oauth_token' => $config['oauth_access_token'], 
    'oauth_timestamp' => time(), 
    'oauth_version' => '1.0' 
); 

$base_info = buildBaseString($base_url, 'GET', array_merge($oauth, $url_arguments)); 
$composite_key = rawurlencode($config['consumer_secret']) . '&' . rawurlencode($config['oauth_access_token_secret']); 
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true)); 
$oauth['oauth_signature'] = $oauth_signature; 

// Make Requests 
$header = array(
    buildAuthorizationHeader($oauth), 
    'Expect:' 
); 
$options = array(
    CURLOPT_HTTPHEADER => $header, 
    //CURLOPT_POSTFIELDS => $postfields, 
    CURLOPT_HEADER => false, 
    CURLOPT_URL => $full_url, 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_SSL_VERIFYPEER => false 
); 

try { 
    $feed = curl_init(); 
    curl_setopt_array($feed, $options); 
    $result = curl_exec($feed); 
    $info = curl_getinfo($feed); 
    curl_close($feed); 
} catch (Exception $e) { 
    die("Error: ".$e); 
} 

// Send suitable headers to the end user. 
if(isset($info['content_type']) && isset($info['size_download'])){ 
    header('Content-Type: '.$info['content_type']); 
    header('Content-Length: '.$info['size_download']); 

} 

echo($result); 
?> 
+0

嗯我發現這個職位:http://stackoverflow.com/questions/17164587/get-search-results-without-user-authentication 它看起來像這是一個Twitter的問題? – REJH

回答

1

我建立this library時有同樣的問題。

This commit具體可能會產生更多的信息。

你基本上存在的問題是你的URL編碼不正確 - 甚至可能是兩次。如果您從上面的源代碼查看代碼(它非常相似,看起來好像已經在那裏修改),那麼它將按照您的想法正常工作。

TLDR:改爲使用上面的單個文件,或者只是複製/粘貼代碼。這幾乎是你想要的。

由於OP發現:他還試圖用%2523(其中%25 = %)替換它。查看上面的庫仍然是可取的。

+0

謝謝你會試試看。在此期間,我得到它與%2523(%25是'%'的網址編碼),現在它的工作。奇怪的是,%40就像這樣,不需要%2540 .. – REJH

+0

我會將你的代碼添加到我的答案中,以便其他用戶可以找到它。 @REJH如果你認爲目前形式的答案足夠好,你可以'勾選'嗎?乾杯。 – Jimbo

+0

Jep你的答案似乎也有效。我將來可能會改用它,看起來更乾淨:) – REJH