【发布时间】:2024-04-12 09:30:03
【问题描述】:
我正在尝试使用 OAuth 1.0 来验证我对某些网络服务的调用,这是我的 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 调用对吗?
【问题讨论】:
-
为什么用 W8 和 WinRT 标记问题?在我看来,它只不过是普通的 C# 和 Google 的库。
-
我尝试将这些标记为 C#4.5(或 5),但找不到标记。我正在使用一些异步操作。
-
我更改了标签。回到这个问题——我看不出有什么问题。怎么了?有什么例外吗?服务返回错误?您是否尝试过检查使用wireshark 传输的内容?
-
谢谢,我从服务器收到一个错误。所以,你是说一切看起来都不错?我应该联系网络服务器管理员。他们有一个网站门户来测试 API,并且它有效。所以我想我做错了什么。
标签: c# oauth .net-4.5 async-await