我正在尝试使用 OAuth 1.0 来验证我对某些 Web 服务的调用,这是我的 C# 代码:
OAuthBase oauth = new OAuthBase();
string normalizedUrl = String.Empty;
string normalizedqueryparameters = String.Empty;
string finalurl;
string sig;
var MerchantId = "xxxx-xxxx-xxxx-xxxx-xxxx";
var PublicKey = "xx";
var SecreteKey = "xxxxx";
var URL = new Uri("http://www.Server.com/DeveloperAPI/category/" + MerchantId + "/NA");
// token secret
sig = oauth.GenerateSignature(URL, PublicKey, SecreteKey, "", "", "POST", oauth.GenerateTimeStamp(), oauth.GenerateNonce(), out normalizedUrl, out normalizedqueryparameters);
finalurl = string.Format("{0}?{1}&oauth_signature={2}", normalizedUrl, normalizedqueryparameters, oauth.UrlEncode(sig));
string text = string.Empty;
using (var clinet = new HttpClient())
{
clinet.DefaultRequestHeaders.Add("Accept", "application/Json");
clinet.DefaultRequestHeaders.Add("Method", "POST");
using (var response = await clinet.GetAsync(finalurl))
{
text = await response.Content.ReadAsStringAsync();
}
}
我从这里得到了 OAuthBase 类:
http://code.google.com/p/oauth/issues/detail?id=223
使用 Fiddler 的原始 http 请求是这样的:
GET http://www.server.com/DeveloperAPI/category/d8d63626-99d7-4aff-abe7-542ad705d083/CK00001277?oauth_consumer_key=Rxxxx7LO&oauth_nonce=65xxx982&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1345xxxx20&oauth_version=1.0&oauth_signature=PhUxxxxOh%2FzIxCo8A%2BQBNxxxxy7E%3D HTTP/1.1
Accept: application/Json
Method: POST
Host: www.xxxx.com
Proxy-Connection: Keep-Alive
我创建 HTTP 调用对吗?