获取Twitter请求令牌失败

我按照http://dev.twitter.com/pages/auth#request-token上的说明进行操作,并开发了ac #class来执行OAuth授权。 我使用了页面上的参数,以及页面上的输出签名基本字符串和签名匹配。 所以我认为算法部分是正确的。 然后我用twitter应用程序中的参数替换了参数,但是我没能从Twitter服务获取请求令牌。 并且响应数据是“无法validationoauth签名和令牌”。

这是我发送的请求(我使用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方法将使用以下内容对参数进行排序:参数来自查询字符串; 来自身体的参数; 参数sepcific为’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参数必须在发送之前进行排序,但签名必须在授权请求的末尾。( http://suuth.net/core/1.0/#anchor14中的 9.1.1)

您可能还需要指定realm =“/ oauth / request_token”。 它是可选的,但正如我记得的那样,Twitter想要这个请求令牌。

如果您可以添加代码,我们可能会发现正在发生的事情,因为您可能无法正确构建请求和签名哈希密钥。