雅虎幻想体育API

问题描述 投票:0回答:2

还有人在使用 Yahoo Fantasy Sports API 吗?我有一个去年运行的应用程序,根本没有更改我的代码,现在当我尝试运行它时,它返回 500 内部错误。

我曾经通过 YQL 控制台进行测试,但现在不再可用。

https://developer.yahoo.com/yql/

有人知道如何在上述网站上发出经过身份验证的请求吗?

我的感觉是,雅虎刚刚停止了对其 FantasySports API 的支持,我认为我将不得不寻找其他解决方案。

想知道是否有其他人以前使用过这个 API 并且是否仍然成功。

oauth-2.0 yahoo yahoo-api
2个回答
0
投票

我弄清楚了如何使用 C# 核心和 Yahoo 的 API。非常感谢这个人

  1. 从 yahoo 获取您的 api 密钥等。

创建一个重定向到请求 URL 的控制器操作,如下所示:

public IActionResult Test()
        {
            yo.yKey = {your Yahoo API key};
            yo.ySecret = {your Yahoo API secret};
            yo.returnUrl = {your return URL as set in the API setup, example "https://website.com/home/apisuccess"};

            var redirectUrl = "https://api.login.yahoo.com/oauth2/request_auth?client_id=" + yo.yKey + "&redirect_uri=" + yo.returnUrl + "&response_type=code&language=en-us";
            return Redirect(redirectUrl);
        }

这会将您发送至通过雅虎进行身份验证的网站。身份验证成功后,它将使用名为 code 的字符串参数将您发送到重定向站点,在示例中它将是 home/apisuccess,因此控制器操作应如下所示:

public async Task<IActionResult> ApiSuccess(string code)
        {        
            List<string> msgs = new List<string>();     //This list just for testing
            /*Exchange authorization code for Access Token by sending Post Request*/
            Uri address = new Uri("https://api.login.yahoo.com/oauth2/get_token");                

            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            byte[] headerByte = System.Text.Encoding.UTF8.GetBytes(_yKey + ":" + _ySecret);
            string headerString = System.Convert.ToBase64String(headerByte);
            request.Headers["Authorization"] = "Basic " + headerString;

            /*Create the data we want to send*/
            StringBuilder data = new StringBuilder();
            data.Append("client_id=" + _yKey);
            data.Append("&client_secret=" + _ySecret);
            data.Append("&redirect_uri=" + _returnUrl);
            data.Append("&code=" + code);
            data.Append("&grant_type=authorization_code");

            //Create a byte array of the data we want to send
            byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());

            // Set the content length in the request headers  
            request.ContentLength = byteData.Length;

            // Write data  
            using (Stream postStream = await request.GetRequestStreamAsync())
            {
                postStream.Write(byteData, 0, byteData.Length);
            }
            // Get response
            var vM = new yOauthResponse();
            string responseFromServer = "";
            try
            {
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    msgs.Add("Into response");
                    // Get the response stream  
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    responseFromServer = reader.ReadToEnd();
                    msgs.Add(responseFromServer.ToString());
                    vM = JsonConvert.DeserializeObject<yOauthResponse>(responseFromServer.ToString());
                }
            }
            catch (Exception ex)
            {
                msgs.Add("Error Occured");
            }
            ViewData["Message"] = msgs;
            return View(vM);
        }

请注意,我使用了该模型的 json 反序列化器,但是您可以对响应执行任何您想要的操作,以从中获取您需要的数据。这是我的 json 模型:

public class yOauthResponse
    {
        [JsonProperty(PropertyName = "access_token")]
        public string accessToken { get; set; }

        [JsonProperty(PropertyName = "xoauth_yahoo_guid")]
        public string xoauthYahooGuid { get; set; }

        [JsonProperty(PropertyName = "refresh_token")]
        public string refreshToken { get; set; }

        [JsonProperty(PropertyName = "token_type")]
        public string tokenType { get; set; }

        [JsonProperty(PropertyName = "expires_in")]
        public string expiresIn { get; set; }
    }

获得该数据后,您需要的主要内容是 access_token,并在控制器操作中按如下方式使用它:

//simple code above removed
    var client = new HttpClient()
                {
                    BaseAddress = new Uri({your request string to make API calls})
                };
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);

                HttpResponseMessage response = await client.GetAsync(requestUri);
                if (response.IsSuccessStatusCode)
                {
                   //do what you will with the response....
                }
                //rest of simple code

希望这对某人有所帮助。快乐编码!


0
投票

截至 2022 年 1 月我的最后一次更新,雅虎确实已弃用并停止了其多个 API,包括 Fantasy Sports API。 YQL 控制台的删除进一步表明支持的停止。因此,尝试发出经过身份验证的请求或使用 API 可能会导致错误或失败。由于雅虎停止支持 Fantasy Sports API,因此可能需要探索替代解决方案或迁移到其他平台。

© www.soinside.com 2019 - 2024. All rights reserved.