Hi All
Been bashing my head against this one for a while so finally decided I needed some help. I’m using C# to make the final POST in the Authorization Code Flow and it always fails with a 404. Here’s the code which I call after grabbing the callback, I know that the callback works because if I don’t call the final POST I can grab the “code” value and make the request using Postman successfully.
public void TwitchAuthorizationApi(string code)
{
HttpWebRequest myWebRequest = null;
ASCIIEncoding encoding = new ASCIIEncoding();
Dictionary<string, string> postDataDictionary = new Dictionary<string, string>();
postDataDictionary.Add("client_id", twitchClientId);
postDataDictionary.Add("client_secret", twitchClientSecret);
postDataDictionary.Add("grant_type", "authorization_code");
postDataDictionary.Add("redirect_uri", twitchRedirectUri);
postDataDictionary.Add("state", "123456");
postDataDictionary.Add("code", code);
string postData = "";
foreach(KeyValuePair<string, string> kvp in postDataDictionary)
{
postData += HttpUtility.UrlEncode(kvp.Key) + "=" + HttpUtility.UrlEncode(kvp.Value) + "&";
}
myTwitchUtils.DebugLog(postData);
byte[] byte1 = encoding.GetBytes(postData);
myWebRequest = WebRequest.CreateHttp("https://api.twitch.tv/kraken/oauth2/token");
myWebRequest.Method = "PUT";
myWebRequest.ContentType = "application/x-www-form-urlencoded";
myWebRequest.ContentLength = byte1.Length;
Stream newStream = myWebRequest.GetRequestStream();
newStream.Write(byte1, 0, byte1.Length);
newStream.Close();
//Exception happens here and I catch the 404 elsewhere in code
WebResponse response = myWebRequest.GetResponse();
newStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(newStream);
var jsonResponse = myStreamReader.ReadToEnd();
myTwitchUtils.DebugLog(String.Format("Response: {0}", jsonResponse));
myStreamReader.Close();
newStream.Close();
response.Close();
return;
}
Any thoughts on what I’ve missed out? I have a theory that it is something to do with the way I am either encoding or placing the data into the POST body but I can’t work out what the problem is. If I take the debug output of the POST body and paste that into Postman the call also succeeds so it must be some subtlety with .Net that I can’t place my finger on
Thanks
Adrian