使用OAuth签署Twitter用户流请求

时间:2011-06-09 13:47:22

标签: oauth twitter twitter-oauth

我正在尝试使用OAuth签署请求。我读过很多关于它的文章,但我现在还不知道如何实际使用它。

我有这个网址:https://userstream.twitter.com/2/user.json我想为某个用户调用它。我有一个应用程序(使用Consumer key en secret)和我想要使用的帐户的API密钥。所以剩下要做的就是签名。 我使用了这个页面:http://hueniverse.com/2008/10/beginners-guide-to-oauth-part-iv-signing-requests/并得到了一个看起来像这样的代码块:

GET /photos?size=original&file=vacation.jpg HTTP/1.1
Host: photos.example.net:80
Authorization: OAuth realm="http://photos.example.net/photos",
    oauth_consumer_key="dpf43f3p2l4k3l03",
    oauth_token="nnch734d00sl2jdk",
    oauth_nonce="kllo9940pd9333jh",
    oauth_timestamp="1191242096",
    oauth_signature_method="HMAC-SHA1",
    oauth_version="1.0",
    oauth_signature="tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D"

然而,cURL不接受这一点。如何构建适用于cURL的东西?

提前致谢

编辑:我真的不需要cURL,只是我可以在GNU / Linux上使用C ++

1 个答案:

答案 0 :(得分:1)

当我从(Windows)命令行卷曲Twitter请求并且必须添加OAuth授权标头时,我就这样做了:

curl -k "https://userstream.twitter.com/2/user.json" --header "Authorization: OAuth realm=""Twitter API"",oauth_consumer_key=""dpf43f3p2l4k3l03"",oauth_token=""nnch734d00sl2jdk"",oauth_nonce=""kllo9940pd9333jh"",oauth_timestamp=""1191242096"",oauth_signature_method=""HMAC-SHA1"",oauth_version=""1.0"",oauth_signature=""tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D"""

当然,如果您使用的是that Java applet,请确保将时间设置为尽可能接近正确的时间,指定OAuth凭据,并在替换输出值之前适当地设置URL部分进入上面的命令行。

Method: GET
URI Scheme: https
Host Name: userstream.twitter.com
Port: 443
Request Path: /2/user.json
Parameters: [remove all]

请注意,在标头输出中,您必须手动将域更改为 Twitter API ,但所有其他输出值应该是正确的。

此外,这个answer有很多可以尝试的C ++ Twitter库,尽管列出的Twitter文档的链接已经过时了 - here's the latest