获取错误400:通过Twitch.tv api进行身份验证时出现错误请求

时间:2016-01-09 17:51:51

标签: c# bad-request

我是新来的,我希望有人可以帮助我。我尝试连接到twitch.tv我试图通过一个小的C#程序在twitch.tv上获得oauth2身份验证。我正在使用twitch.tv authentication request.这是我的C#代码:

    var loginURL = "https://api.twitch.tv/kraken/oauth2/authorize?
                               response_type=code&"+
                               client_id="+ clientID+"
                               "&redirect_uri=http://localhost&"+
                               "state=TWStreamingStateAuthenticated";                    
    this.richTextBox1.Text = loginURL;
    string code = get_DownLoadString(loginURL);
    this.richTextBox1.Text = code;

这是不起作用的部分。它给了我错误400:错误请求

    WebRequest request = WebRequest.Create("https://api.twitch.tv/kraken/oauth2/token");
    request.Method = "POST";
    string postData = "client_id=" + clientID +
                      "&client_secret=" + clientSecret +
                      "&grant_type=authorization_code" +
                      "&redirect_uri=http://localhost" +
                      "&code=" + code +
                      "&state=TWStreamingStateAuthenticated";

    ASCIIEncoding encoding = new ASCIIEncoding();
    postData = HttpUtility.UrlEncode(postData);            
    byte[] byteArray = encoding.GetBytes(postData);
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = byteArray.Length;
    Stream datatream = request.GetRequestStream();
    datatream.Write(byteArray, 0, byteArray.Length);
    datatream.Close();
    WebResponse respone = request.GetResponse();
    MessageBox.Show(((HttpWebResponse)respone).StatusDescription);

我希望有人可以帮助我。 这是Get_DownloadString(字符串URL)方法。

private static string get_DownLoadString(string URL)
{
    try
    {
        string temp = (new WebClient().DownloadString(URL));
        return temp;
    }
    catch (WebException)
    {
        return null;
    }
}

1 个答案:

答案 0 :(得分:2)

此代码对我来说并不合适:

    string postData = "client_id=" + clientID +
                      "&client_secret=" + clientSecret +
                      "&grant_type=authorization_code" +
                      "&redirect_uri=http://localhost" +
                      "&code=" + code +
                      "&state=TWStreamingStateAuthenticated";

    ASCIIEncoding encoding = new ASCIIEncoding();
    postData = HttpUtility.UrlEncode(postData);
    byte[] byteArray = encoding.GetBytes(postData);
    // ...

您正在对整个后期数据字符串进行URL编码。这样可以将发布数据中的&=符号分别转换为%26%3d。当远程服务器收到此数据时,它将扫描它以查找&=符号,以便分离出参数名称和值。当然,它不会找到任何,所以它会假设你有一个没有值的大参数名称。服务器可能期望您尝试发送的六个参数中的每个参数的值,但是看不到它们的值,这可能就是您收到400 Bad Request错误的原因。

URL编码参数值可能包含字母和数字以外的字符,而不是对整个字符串进行URL编码。我会尝试以下方式:

    string postData = "client_id=" + HttpUtility.UrlEncode(clientID) +
                      "&client_secret=" + HttpUtility.UrlEncode(clientSecret) +
                      "&grant_type=authorization_code" +
                      "&redirect_uri=" + HttpUtility.UrlEncode("http://localhost") +
                      "&code=" + HttpUtility.UrlEncode(code) +
                      "&state=TWStreamingStateAuthenticated";

    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] byteArray = encoding.GetBytes(postData);
    // ...

这样,远程服务器仍会看到&=个字符,因此可以提取参数名称和值。因为我们对客户端ID,客户端密码,URL和代码进行了URL编码,所以它们包含的任何可能在URL中有意义的字符都不具有该含义,并且将由远程服务器按预期接收。

此外,如果您仍然收到400 Bad Request错误响应,请尝试通过在响应上调用GetResponseStream()来读取响应流的内容。通常,这将包含一条消息,可以帮助您找出问题所在。

仔细查看代码后,您似乎对OAuth身份验证的工作方式存在误解。您的getDownload_String方法无法获取所需的访问代码,只能获取Twitch登录页面的HTML文本。

这是OAuth身份验证的工作原理:

  1. 您的应用会将用户发送到登录网址,以允许用户登录Twitch。
  2. 在Web浏览器中,用户然后输入他们的登录凭据并将页面提交给Twitch。
  3. 然后,Twitch API通过将用户的Web浏览器重定向到重定向URL来响应,并附加代码。然后,您的网络应用会从网址中读取此代码。
  4. 如果您的代码位于Web应用程序中,则它将能够响应在步骤3中重定向到的URL。或者,您可以使用WebBrowser控件(Windows FormsWPF)处理Twitch登录,并处理Navigating事件。如果要导航的URL以重定向URL开头,请从URL中获取代码,取消导航并隐藏登录Web浏览器控件。

    RichTextBox控件的存在以及您对代码作为小型C#应用程序的评论使我认为您的代码是Windows窗体或WPF应用程序。如果是这种情况,那么您需要:

    • 如上所述使用WebBrowser控件,
    • 使用网络应用程序替换您的WinForms / WPF应用程序,或
    • 与Twitch联系以请求使用password flow(似乎不需要重定向),然后使用它。