php - Twitter Streaming 401 Unauthorized OAuth -
i'm trying create script uses twitter streaming api keep tab on tweets hash-tag.
whenever try creating request though, 401 unauthorized return. can me figure out i'm doing incorrectly?
i tried follow twitter's api dot, apparently i'm doing wrong. code have 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 figured out. combination of poor string manipulation , not percent encoding everything. both keys , values of oauth string must percent encoded. had done in signature, not in actual request. once fixed that, request got through , connected twitter streaming api.