3

我正在尝试将 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 用户名,并能够获取该用户的频道和提要。

4

1 回答 1

1

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

于 2013-06-09T17:28:22.620 回答