Skip to main content
 首页 » 编程设计

c#之获取 Twitter 请求 token 失败

2024年02月23日12mfryf

我按照 http://dev.twitter.com/pages/auth#request-token 上的说明进行操作, 并开发了一个 c# 类来做 OAuth 授权。 我使用了页面上的参数,输出的签名基础串和签名与页面上的一致。所以我认为算法部分是正确的。 然后我将参数替换为我的推特应用程序中的参数,但我未能从推特服务获取请求 token 。响应数据为“无法验证 oauth 签名和 token ”。

这是我发送的请求(我使用 http,而不是 https 进行调试):

POST http://api.twitter.com/oauth/request_token HTTP/1.1 
Content-Type: application/x-www-form-urlencoded 
Authorization: OAuth oauth_callback="http%3A%2F%2Flocalhost%3A3005%2Fthe_dance%2Fprocess_callback%3Fservice_provider_id%3D11", oauth_consumer_key="GDdmIQH6jhtmLUypg82g", oauth_nonce="QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1272323042", oauth_version="1.0", oauth_signagure="IP%2FEEoc4tKdiobM%2FKH5cPK69cJM%3D" 
Host: api.twitter.com 
Proxy-Connection: Keep-Alive 

这是回复:

HTTP/1.1 401 Unauthorized 
Connection: Keep-Alive 
Connection: Proxy-Support 
Content-Length: 44 
Via: 1.1 APS-PRXY-09 
Expires: Tue, 31 Mar 1981 05:00:00 GMT 
Date: Fri, 08 Apr 2011 05:47:20 GMT 
Content-Type: text/html; charset=utf-8 
Server: hi 
Proxy-Support: Session-Based-Authentication 
Status: 401 Unauthorized 
X-Transaction: 1302241640-40339-46793 
Last-Modified: Fri, 08 Apr 2011 05:47:20 GMT 
X-Runtime: 0.01519 
Pragma: no-cache 
X-Revision: DEV 
Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0 
Set-Cookie: k=207.46.55.29.1302241640766556; path=/; expires=Fri, 15-Apr-11 05:47:20 GMT; domain=.twitter.com 
Set-Cookie: guest_id=13022416407746962; path=/; expires=Sun, 08 May 2011 05:47:20 GMT 
Set-Cookie: _twitter_sess=BAh7CDoPY3JlYXRlZF9hdGwrCEiBpjMvAToHaWQiJWMzMTViOGZiNDkzMDRi%250ANjNhMmQwYmVkZDBhNTc2NTc4IgpmbGFzaElDOidBY3Rpb25Db250cm9sbGVy%250AOjpGbGFzaDo6Rmxhc2hIYXNoewAGOgpAdXNlZHsA--177afd5c0f6fe30005ab9a9412e6f85ab03cbfa7; domain=.twitter.com; path=/; HttpOnly 
Vary: Accept-Encoding 
 
Failed to validate oauth signature and token 

这就是我生成规范化参数的方式:

string.Join("&", (from d in this.BuildParameterDict() 
                  select string.Format("{0}={1}", OAuthEncoding.Encode(d.Key), OAuthEncoding.Encode(d.Value)))) 

BuildParameterDict 方法将排序构建一个列表:来自查询字符串的参数;来自 body 的参数;特定于“oauth”的参数,“oauth_signature”除外。

然后生成签名基础字符串:

            StringBuilder sb = new StringBuilder(); 
 
            sb.Append(OAuthEncoding.Encode(this._request.Method)); 
            sb.Append('&'); 
            sb.Append(OAuthEncoding.Encode(this.GetNormalUri())); 
            sb.Append('&'); 
            sb.Append(OAuthEncoding.Encode(this.GetNormalParameters())); 

这是生成的带有上述页面参数的基本字符串:

POST&https%3A%2F%2Fapi.twitter.com%2Foauth%2Frequest_token&oauth_callback%3Dhttp%253A%252F%252Flocalhost%253A3005%252Fthe_dance%252Fprocess_callback%253Fservice_provider_id%253D11%26oauth_consumer_key%3DGDdmIQH6jhtmLUypg82g%26oauth_nonce%3DQP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1272323042%26oauth_version%3D1.0

与该页面上的字符串相同。

请您参考如下方法:

您的 oauth 签名在您的请求中列为“oauth_signagure”。

oAuth 参数必须在发送前排序,但签名必须在授权请求的末尾。(9.1.1 in http://oauth.net/core/1.0/#anchor14)

您可能还需要指定一个 realm="/oauth/request_token"。它是可选的,但我没记错的话,Twitter 希望用这个作为请求 token 。

如果您可以添加您的代码,我们可能会发现发生了什么,因为您可能没有正确构建请求和签名散列 key 。