gpt4 book ai didi

c# - 无法查询 Yahoo!梦幻体育API

转载 作者:行者123 更新时间:2023-11-30 14:52:01 24 4
gpt4 key购买 nike

我正在尝试从 Yahoo! 获取详细信息使用 OAuth2.0 的梦幻体育 API。我获得了 access_token 以使用 YQL 进行查询。我的代码

using (var client = new System.Net.WebClient())
{
client.Headers.Add("Authorization", "Bearer " + response.access_token);
var query = "select%20*%20from%20fantasysports.games%20where%20game_key%3D'nfl'";
Response.Write(query);
var url = String.Format("https://query.yahooapis.com/v1/yql?q={0}&format=json&diagnostics=true&callback=", query);
output = client.DownloadString(url);
}

我的回应

{
"query": {
"count": 0,
"created": "2015-09-27T17:39:48Z",
"lang": "en-US",
"diagnostics": {
"publiclyCallable": "true",
"url": {
"execution-start-time": "4",
"execution-stop-time": "137",
"execution-time": "133",
"http-status-code": "401",
"http-status-message": "Authorization Required",
"content": "http://fantasysports.yahooapis.com/fantasy/v2/games;game_keys=nfl"
},
"user-time": "138",
"service-time": "133",
"build-version": "0.2.240"
},
"results": null
}
}

我收到了一条需要授权状态消息。

我认为它必须对我的请求 header 做些什么。有人可以帮助我理解为什么我的请求在这里被拒绝吗?

最佳答案

雅虎!有两个 OAuth 授权流程

  1. 双足流
  2. 三足流

雅虎笔记,

Most of the Fantasy API data relies on 3-legged OAuth, as much of thedata is specific to a certain Yahoo! user. However, you can use2-legged OAuth to request purely public data. 2-legged OAutheffectively boils down to making a request without setting an accesstoken through the default PHP OAuth library, or effectively using yourconsumer key/secret as the token.

所以我们应该去找three-legged flow of OAuth authorization正如 Yahoo! 中所描述的那样文档。在授权流程结束时,您将获得 oauth_tokenoauth_token_secret

Yahoo 在 C# ( Link here ) 中提供了此代码。

public static string GetUserDataFromYahoo(string requestEndPoint, string token, string tokenSecret)
{
var data = String.Empty;
var uri = new Uri(requestEndPoint);
string url, param;
var oAuth = new OAuthBase();
var nonce = oAuth.GenerateNonce();
var timeStamp = oAuth.GenerateTimeStamp();
var signature = oAuth.GenerateSignature(
uri,
consumerKey,
consumerSecret,
token,
tokenSecret,
"GET",
timeStamp,
nonce,
OAuthBase.SignatureTypes.HMACSHA1,
out url,
out param);
data = String.Format("{0}?{1}&oauth_signature={2}", url, param, signature);
var requestParametersUrl = String.Format("{0}?{1}&oauth_signature={2}", url, param, signature);
var request = WebRequest.Create(requestParametersUrl);
using (var response = request.GetResponse())
using (Stream dataStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(dataStream))
{
data = reader.ReadToEnd();
}
return data;
}

此代码使用此 OAuthBase.cs class .

当你使用这段代码时,你会得到一个

OST_OAUTH_SIGNATURE_INVALID_ERROR

那是因为,OAuthBase.cs 有一个 bug that's been noted here .要更正您必须这样做。

Line 199 (in NormalizeRequestParameters method) must change from:

sb.AppendFormat("{0}={1}", p.Name, p.Value);

to

sb.AppendFormat("{0}={1}", UrlEncode(p.Name), UrlEncode(p.Value));

编码愉快!

关于c# - 无法查询 Yahoo!梦幻体育API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32815568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com