c# - C# 中的 Twitch TV OAuth 登录

标签 c# asp.net-mvc-3 wcf justin.tv

我正在尝试将 twitch TV 帐户连接到我网站上的用户个人资料,但出现 403 Forbidden 错误。我正在尝试使用此处指定的授权代码流程:https://github.com/justintv/Twitch-API/blob/master/authentication.md#auth-code但是我必须发回 Twitch TV 的第二部分是我收到错误的地方。我正在使用 ASP.net MVC3 和 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 用户名,并能够获取该用户的 channel 和提要。

最佳答案

我不太擅长这个,但我认为问题在于您正尝试通过服务器端口连接到本地主机,这是您自己的计算机。如果这不是问题,这就是您想要的。您是否考虑过端口转发?

关于c# - C# 中的 Twitch TV OAuth 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17011716/

相关文章:

c# - WebBrowser.InvokeScript() 抛出错误 : 80020006

asp.net-mvc-3 - 列模板剑道ui网格mvc Action 链接

html - 设置可选的禁用属性

c# - 提高正则表达式的性能

c# - 使用 LINQ 在运行时从数据库中获取数据

asp.net-mvc - MVC 3 和强类型 View

c# - IIS 托管 WCF 服务中的 Log4Net

c# - 无法使用 https 端点创建 wcf web 服务

c# - 如何在 ASP.NET Core 1 和 VSCode 编辑器中使用 WCF 服务?

c# - Parallel.ForEach 和 DataTable - DataTable.NewRow() 不是线程安全的 "read"操作吗?