简体   繁体   English

Twitter流401未经授权的OAuth

[英]Twitter Streaming 401 Unauthorized OAuth

I'm trying to create a script that uses the twitter streaming api to keep a tab on tweets with a certain hash-tag. 我正在尝试创建一个使用Twitter流API的脚本,以在带有特定哈希标签的推文上保留一个选项卡。

Whenever I try creating a request though, I always get a 401 Unauthorized return. 但是,无论何时尝试创建请求,我总是会得到401未经授权的退货。 Can anyone help me figure out what I'm doing incorrectly? 谁能帮我弄清楚我做错了什么?

I tried to follow twitter's api to the dot, but apparently I'm doing something wrong. 我试图按照Twitter的api进行操作,但显然我做错了。 The code I have is below. 我的代码如下。

$base_url_string = urlencode($base_url);
$parameter_string = urlencode('oauth_consumer_key') . '=' . urlencode($consumer_key) . '&'
    . urlencode('oauth_nonce') . '=' . urlencode($nonce) . '&'
    . urlencode('oauth_signature_method') . '=' . urlencode($signature_method) . '&'
    . urlencode('oauth_timestamp') . '=' . urlencode($timestamp) . '&'
    . urlencode('oauth_token') . '=' . urlencode($token) . '&'
    . urlencode('oauth_version') . '=' . urlencode($version) . '&'
    . urlencode('track') . '=' . urlencode('#kitten');


$signature_base_string = $method . "&" . $base_url_string . "&" . urlencode($parameter_string);

$signature = base64_encode(hash_hmac('sha1', $signature_base_string, $secret, true));

$fp = fsockopen("ssl://stream.twitter.com", 443, $errno, $errstr, 30);

if (!$fp) {

    print "$errstr ($errno)\n";

} else {

$request = $method . " /1.1/statuses/filter.json?" . urlencode("track") . "=" . urlencode("#kitten") . " HTTP/1.1\r\n";
$request .= "Host: stream.twitter.com\r\n";
$request .= 'Authorization: OAuth'
        . ' oauth_consumer_key="' . $consumer_key . '",'
        . ' oauth_nonce="' . $nonce . '",'
        . ' oauth_signature="' . $signature . '",'
        . ' oauth_signature_method="' . $signature_method . '",'
        . ' oauth_timestamp="' . $timestamp . '",'
        . ' oauth_token="' . $token . '",'
        . ' oauth_version="' . $version . '"'
        . "\r\n\r\n";

print $request . "</br>";

fwrite($fp, $request);

while (!feof($fp)) {

    $json = fgets($fp);
    echo $json;
    $data = json_decode($json, true);

    if ($data) {

        print $data;
    }
}

print 'exiting...';
  fclose($fp);
}

Ok so I figured this all out. 好的,所以我想通了。 It was a combination of poor string manipulation and not Percent Encoding everything. 这是不良的字符串处理方式与并非所有内容都进行百分比编码的结合。 Both the keys and values of the OAuth string must be Percent encoded. OAuth字符串的键和值都必须进行百分比编码。 I had done that in the signature, but not in the actual request. 我是在签名中完成的,但不是在实际请求中完成的。 Once I fixed that, the request got through and I was connected to the Twitter Streaming API. 修复此问题后,请求通过,并连接到Twitter Streaming API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM