搜索主题标签时Twitter api 1.1 oauth失败

时间:2013-06-28 13:30:20

标签: php javascript twitter

我正在使用我在互联网上找到的修改过的php脚本作为javascript和twitter api 1.1之间的代理(我需要这样做,因为javascript不能执行oauth操作,而api 1.1要求完全相同:认证)。

脚本工作正常 - 直到我搜索主题标签,然后oauth失败。

以下是我在搜索@ z25org

时从twitter获得的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:未经授权的访问。杰森:

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

1 个答案:

答案 0 :(得分:1)

构建this library时,我遇到了同样的问题。

This commit特别可能会提供更多信息。

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

TLDR:请改用上述单个文件,或者只复制/粘贴代码。这几乎是你想要的。

OP发现:他还尝试将其替换为%2523(其中%25 = %)。我们仍然建议您查看上面的图书馆。