无法查询 Yahoo!梦幻运动 API

Unable to query Yahoo! Fantasy Sports API

我正在尝试从 Yahoo! 获取详细信息梦幻体育 API 使用 OAuth2.0。我得到了 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。有人可以帮助我理解为什么我的请求在这里被拒绝吗?

我建议您没有正确收到access_token。这就是您在调用服务器时得到 Authorization Required 的原因。

您需要检查获取 access_token

的代码

雅虎!有两个 OAuth 授权流程

  1. 双足流
  2. 三足流

雅虎笔记,

Most of the Fantasy API data relies on 3-legged OAuth, as much of the data is specific to a certain Yahoo! user. However, you can use 2-legged OAuth to request purely public data. 2-legged OAuth effectively boils down to making a request without setting an access token through the default PHP OAuth library, or effectively using your consumer 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));

编码愉快!