oauth2.0 雅虎梦幻体育API

d7v8vwbk  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(85)

有人还在使用雅虎梦幻体育API吗?我有一个应用程序,去年工作,没有改变我的代码在所有,现在它是返回一个500内部错误,当我试图运行它。
我过去常常通过YQL控制台测试,但现在已经没有了。
https://developer.yahoo.com/yql/
有人知道如何在上面的网站上进行身份验证请求吗?
我的感觉是,雅虎刚刚停止支持他们的幻想体育API,我将不得不寻找其他的解决方案,我认为。
想知道是否有人在那里使用这个API以前,是或不是仍然有成功与它。

9rnv2umw

9rnv2umw1#

我弄明白了如何使用C#核心和雅虎的API。非常感谢this guy
1.从雅虎获取你的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);
        }

字符串
这将把你发送到一个通过Yahoo认证的站点。在成功认证后,它将把你发送到一个名为code的字符串参数的重定向站点,在示例中它将是home/apissuccess,因此控制器操作应该如下所示:

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


希望这对某个地方的人有帮助。编码快乐!

相关问题