我目前正在使用 oauth.net 上提供的 OAuthBase.cs 帮助程序类来实现 OAuth 来与 Yahoo!幻想API。我目前陷入了第 5 步(使用访问 token /访问 key 调用 API 服务)。
我成功完成了Step 4但似乎无法创建实际的服务调用。 The documentation是有限的;我应该使用什么参数?我收到 401 或 400 http 错误。我通过以下方式生成签名:
url = new Uri("http://query.yahooapis.com/v1/public/yql?q=select * from fantasysports.teams.roster.stats where team_key='nba.l.52669.t.5' and week='5' and stats_type='week' and stats_week='5'&format=json");
signature = oauth.GenerateSignature(url, string.Empty, consumerKey, consumerSecret, accessToken, accessTokenSecret, "GET", time, string.Empty, nonce, OAuth.OAuthBase.SignatureTypes.HMACSHA1, out normalizedUrl, out normalizedRequestParameters);
using (var y = WebRequest.Create(string.Format("{0}?{1}&oauth_signature={2}", normalizedUrl, normalizedRequestParameters, signature)).GetResponse())
{
....
}
其中 url
是我尝试进行的 api 调用,consumerKey
/consumerSecret
是我注册时提供给我的 key 对于 Yahoo!,accessToken
/accessTokenSecret
是第 4 步中从 request_auth
返回的响应。我做错了什么?
提前致谢
编辑:12/14 - 对于那些不熟悉 OAuthBase 的人来说,它本质上是一个通过以下方式生成签名的库 1. 合并所有url/参数(consumerkey、token、tokenSecret、httpMethod、nonce、time等),排序,并对url/参数进行规范化; 2、将consumerSecret'&'tokenSecret编码为HMACSHA1 key ; 3.计算hmacsha1 key 的哈希
最佳答案
这里是一些使用 OAuth 访问 Yahoo API 的工作代码(在本例中为 BOSS Geo API)
[Test]
public void MakeCallToBossGeoApi()
{
string result;
var uri = new Uri(@"http://yboss.yahooapis.com/geo/placefinder?country=SE&flags=J&locale=sv_SE&postal=41311");
var o = new OAuthBase();
string nonce = o.GenerateNonce();
var timestamp = o.GenerateTimeStamp();
string normalizedUrl;
string normalizedParameters;
string signature = HttpUtility.UrlEncode(
o.GenerateSignature(uri,
"yourconsumerkeyhere",
"yourconsumersecrethere", null, null, "GET",
timestamp, nonce, out normalizedUrl,
out normalizedParameters));
uri = new Uri(normalizedUrl +"?"+ normalizedParameters + "&oauth_signature=" + signature );
using (var httpClient = new WebClient())
{
result = httpClient.DownloadString(uri.AbsoluteUri);
}
Console.WriteLine(result);
}
关于c# - 带有 OAuthBase.cs 的雅虎 Oauth API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13815226/