Twitter api 1.1 oauth在搜索标签时失败


Twitter api 1.1 oauth fails when searching hashtags

我正在使用我在互联网上找到的一个修改过的php脚本来充当javascript和twitter api 1.1之间的代理(我需要这样做,因为javascript不能执行oauth操作,而api 1.1正需要这样做:身份验证)。

脚本运行良好,直到我搜索一个标签,然后oauth失败。

以下是我在搜索@z25org时从推特上得到的curl_info的一个例子

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)。但当我搜索标签时:

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);
?>

我在构建这个库时也遇到了同样的问题。

这种提交可能会产生更多信息。

基本上,你遇到的问题是URL编码错误,甚至可能两次。如果您从上面的源代码中查看代码(它非常相似,似乎在那里进行了修改),那么它将完全按照您的意愿工作。

TLDR:使用上面的单个文件,或者只复制/粘贴其中的代码。这几乎是您想要的。

正如OP所发现的:他还尝试用%2523(其中%25=%)代替它。最好还是去上面的图书馆看看。