c#中的Twitch TV OAuth登录

本文关键字:OAuth 登录 TV Twitch 中的 | 更新日期: 2023-09-27 18:17:30

我正试图将twitch TV帐户连接到我网站上的用户配置文件,我正在获得403禁止错误。我试图使用这里指定的授权代码流:https://github.com/justintv/Twitch-API/blob/master/authentication.md#auth-code但第二部分,我必须回到Twitch电视是我得到的错误。我用ASP.net mvc和c#来做这件事。

这是我的方法来获得代码,并要求用户给我的应用程序访问twitch TV(这工作如预期):

  [Authorize]
  public ActionResult TwitchTvLogOn(string returnUrl)
  {
     string redirectUrl = "";
     // This is special code used to determine the URL that will be used when working in UGDB since the URL is different in 
     // development than it is in production.
     #if (DEBUG)
        redirectUrl = "http://localhost:58386/Account/AuthorizeTwitchTv";
     #else
        redirectUrl = "http://www.mywebsite.com/Account/AuthorizeTwitchTv";
     #endif
     var loginUri = "https://api.twitch.tv/kraken/oauth2/authorize?response_type=code&client_id=" +
                    System.Configuration.ConfigurationManager.AppSettings["TwitchClientId"] +
                    "&redirect_uri=" + redirectUrl + "&state=" + returnUrl;
     return Redirect(loginUri);
  }

这是不能正常工作的部分,并给出403:

  public ActionResult AuthorizeTwitchTv(string code, string state)
  {
     string currentUrl = Request.Url.AbsoluteUri;
              string redirectUrl = "";
     #if (DEBUG)
        redirectUrl = "http://localhost:58386/Account/AuthorizeTwitchTv";
     #else
        redirectUrl = "http://www.mywebsite.com/Account/AuthorizeTwitchTv";
     #endif
     var twitchTvPost = "https://api.twitch.tv/kraken/oauth2/token?client_id=" +
                             System.Configuration.ConfigurationManager.AppSettings["TwitchClientId"] + "&client_secret=" +
                             System.Configuration.ConfigurationManager.AppSettings["TwitchAppSecret"] + "&grant_type=authorization_code&redirect_uri=" + 
                             redirectUrl + "&code=" + code;
     ASCIIEncoding encoding = new ASCIIEncoding();
     string postData = "client_id=" + System.Configuration.ConfigurationManager.AppSettings["TwitchClientId"];
     postData += ("&client_secret=" + System.Configuration.ConfigurationManager.AppSettings["TwitchAppSecret"]);
     postData += ("&grant_type=authorization_code");
     postData += ("&redirect_uri=" + redirectUrl);
     postData += ("&code=" + code);
     byte[] data = encoding.GetBytes(postData);
     // Prepare POST web request...
     HttpWebRequest myRequest =
       (HttpWebRequest)WebRequest.Create(new Uri("https://api.twitch.tv/kraken/oauth2/token"));
     myRequest.Method = "POST";
     myRequest.ContentType = "application/x-www-form-urlencoded";
     myRequest.ContentLength = data.Length;
     Stream newStream = myRequest.GetRequestStream();
     // Send the data.
     newStream.Write(data, 0, data.Length);
     newStream.Close();
     // Get response  
     HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
     // Get the response stream  
     StreamReader reader = new StreamReader(response.GetResponseStream());
     // Read the whole contents and return as a string  
     string result = reader.ReadToEnd();

     return View();
  }

任何帮助都将非常感激。总的最终目标是获得"access_token",这样我就可以使用它来获取当前用户的twitch用户名,并能够抓取该用户的频道和提要。

c#中的Twitch TV OAuth登录

我对此不是很好,但我认为问题是你正试图通过服务器端口连接到本地主机,这是你自己的计算机。如果这不是问题,这是你想要的。你考虑过港口转发吗?