【发布时间】:2016-04-18 02:43:14
【问题描述】:
我是 Erlang 世界的新手,我正在尝试为 Twitter Stream API 编写一个客户端。我正在使用 httpc:request 发出 POST 请求,但我经常收到 401 错误,我显然在发送请求的方式上做错了......我所拥有的看起来像这样:
fetch_data() ->
Method = post,
URL = "https://stream.twitter.com/1.1/statuses/filter.json",
Headers = "Authorization: OAuth oauth_consumer_key=\"XXX\", oauth_nonce=\"XXX\", oauth_signature=\"XXX%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"XXX\", oauth_token=\"XXX-XXXXX\", oauth_version=\"1.0\"",
ContentType = "application/json",
Body = "{\"track\":\"keyword\"}",
HTTPOptions = [],
Options = [],
R = httpc:request(Method, {URL, Headers, ContentType, Body}, HTTPOptions, Options),
R.
在这一点上,我确信签名没有问题,因为在尝试使用 curl 访问 API 时,相同的签名可以正常工作。我猜我提出请求的方式存在一些问题。
按照上述方式提出的请求,我得到的响应是:
{ok,{{"HTTP/1.1",401,"Unauthorized"},
[{"cache-control","must-revalidate,no-cache,no-store"},
{"connection","close"},
{"www-authenticate","Basic realm=\"Firehose\""},
{"content-length","1243"},
{"content-type","text/html"}],
"<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n<title>Error 401 Unauthorized</title>\n</head>\n<body>\n<h2>HTTP ERROR: 401</h2>\n<p>Problem accessing '/1.1/statuses/filter.json'. Reason:\n<pre> Unauthorized</pre>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n</body>\n</html>\n"}}
当我尝试使用 curl 时,我正在使用这个:
curl --request 'POST' 'https://stream.twitter.com/1.1/statuses/filter.json' --data 'track=keyword' --header 'Authorization: OAuth oauth_consumer_key="XXX", oauth_nonce="XXX", oauth_signature="XXX%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="XXX", oauth_token="XXX-XXXX", oauth_version="1.0"' --verbose
而且我得到的事件就好了。
非常感谢您对此提供的任何帮助,这是 Erlang 的新手,我已经在这个问题上苦苦挣扎了很长一段时间。
【问题讨论】:
-
我使用 oauth:get/6 (github.com/tim/erlang-oauth) 在
statuses/sample连接到他们的流,您可以使用oauth:post进行过滤流。
标签: twitter stream erlang client