Je suis nouveau ici et j'espère que quelqu'un peut m'aider. J'essaie de me connecter à twitch.tv J'essaye d'obtenir une authentification oauth2 sur twitch.tv avec un petit programme C#. J'utilise le twitch.tv authentication request. Voici mon code C#:Obtenir Erreur 400: Mauvaise demande à l'authentification via Twitch.tv api
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;
Ceci est la partie, qui ne fonctionne pas. Il me donne le Erreur 400: Bad Request.
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);
J'espère que quelqu'un peut m'aider. Et voici la méthode Get_DownloadString (string URL).
private static string get_DownLoadString(string URL)
{
try
{
string temp = (new WebClient().DownloadString(URL));
return temp;
}
catch (WebException)
{
return null;
}
}
Je le fais comme tu dis. Mais sache que je reçois le 403: interdit. Mais je suis sûr que c'est mon clientID et mon clientSecret correct. Parce que je le copie de mon canal Twitch. – Innoszorn
@Innoszorn: Je pense que c'est un pas dans la bonne direction. Le serveur comprend maintenant votre requête mais refuse l'accès. Comment obtenez-vous le 'code'? Pourriez-vous s'il vous plaît modifier votre question pour inclure la méthode 'get_DownLoadString'? Aussi, comme mentionné dans le dernier paragraphe que j'ai écrit, y a-t-il un message dans le corps de la réponse? –
J'ai inclus la méthode get_DownloadString. La partie du code: request.GetResponse() déclenche l'exception: 403: Interdit. – Innoszorn