2015-09-28 32 views
3

我想從雅虎拉細節!使用OAuth2.0的Fantasy Sports API。我得到了access_token使用YQL進行查詢。我的代碼無法查詢Yahoo! Fantasy體育API

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 
    } 
} 

我正在一個需要授權狀態消息。

我認爲它必須做我的請求標題。有人能幫助我理解爲什麼我的請求在這裏被拒絕嗎?

+0

'Response.Write(query);'看起來很狡猾。 –

+0

@RichardSchneider:沒有其他更簡單的方式雅虎!不會讓我在本地測試Fantasy Sports API – naveen

+0

@naveen,你有沒有得到這個工作,所以你可以從雅虎體育使用C#拉動公共數據?我正在努力,但發現它非常困難。我試圖運行這個YQL命令:select * from fantasysports.players其中game_key = 371,這應該不是我使用的RESTful命令:https://query.yahooapis.com/v1/public/yql?q=select %20 *%20 from%20fantasysports.players%20where%20game_key%3D371&diagnostics = true到目前爲止,您的帖子在這裏似乎是最有前途的,但我覺得你沒有列出你的整個代碼,只有片段,所以我不知道如何繼續 – dave317

回答

2

雅虎有兩個OAuth的授權流程

  1. 兩個腿流
  2. 三個條腿的流動

雅虎筆記,

大多數幻想API數據的依賴於三足式OAuth,儘可能多 數據是特定於某個Yahoo!用戶。但是,您可以使用 雙腿OAuth來請求純公共數據。雙腿OAuth 實際上可以歸結爲制定請求,而無需通過默認PHP OAuth庫設置訪問令牌 ,或者有效地使用消費者密鑰/密鑰作爲令牌。

所以我們應該去找three-legged flow of OAuth authorization,就像在Yahoo!文檔。在授權流程結束時,您將獲得oauth_tokenoauth_token_secret

雅虎已在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。要糾正你必須這樣做。

線199(在NormalizeRequestParameters方法)必須更改:

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

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

編碼愉快!

+0

發佈此代碼可能會遇到這些問題,同時從雅虎提取數據的編碼器。 – naveen

+0

OAuthBase.cs鏈接已停用請嘗試https://code.google.com/archive/p/oauth/issues/138#c4 –

+1

請解釋如何使用GetUserDataFromYahoo。你是說與雅虎代碼一起使用它嗎?作爲第三回合? –

2

我建議你沒有正確收到access_token。這就是爲什麼你在撥打服務器時得到Authorization Required

你需要檢查你的代碼,獲取access_token

+0

我正在獲取access_token,refresh_token整個事情。它與呼叫有關。 – naveen

+0

是的。部分原因在於此。感謝 – naveen