我依照指示在http://dev.twitter.com/pages/auth#request-token,并制定了C#类做OAuth授权。 我使用了页面上的参数,输出签名基础字符串和签名与页面上的参数匹配。所以我认为算法部分是正确的。然后我用我的twitter应用程序中的参数替换参数,但是我未能从Twitter服务获取请求令牌。响应数据是“无法验证oauth签名和令牌”。收购Twitter请求令牌失败
这是我发送请求(我用的,而不是HTTPS的调试HTTP):
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方法将s orted创建一个列表:来自查询字符串的参数;来自身体的参数;参数指定为'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_signatu re_method%3DHMAC-SHA1%26oauth_timestamp%3D1272323042%26oauth_version%3D1.0
它与该页面上的字符串相同。
你的基本字符串似乎没问题。你的哈希怎么样?你在创建签名时使用了什么?密钥需要从OAUTH_CONSUMER_SECRET和OAUTH_TOKEN构建。即使令牌缺少关键需要有一个与末(代码是PHP):'$ KEY = $ OAUTH_CONSUMER_SECRET '&' $组oauth_token;'比签名'$ OAUTH_SIGNATURE = BASE64_ENCODE(hash_hmac(“SHA1。 ',$ BASE_STRING,$ KEY,true));'...我想你的签名有问题。 – emrahgunduz 2011-04-08 11:24:34
此外,您创建的字符串似乎与Twitter示例中的“完全一样”。你是不是自己创建一个随机数和时间戳?你使用手写字符串吗?您必须提供时间戳作为unixtime,并且随机数必须是随机的。像md5哈希一个随机大数字... – emrahgunduz 2011-04-08 11:31:32
是的,'&'已经照顾好了。在上面的代码中,我硬编码'timespan'和'nonce',实际上它们被替换为正确的值。 – davidshen84 2011-04-08 13:23:59