I am trying to connect a twitch TV account to a user profile on my website and I am getting a 403 Forbidden error. I am trying to use the Authorization Code Flow specified here: https://github.com/justintv/Twitch-API/blob/master/authentication.md#auth-code but the 2nd part where I have to Post back to Twitch TV is where I am getting the error. I am doing this with ASP.net MVC3 and C#.
Here is my method to get the code and ask the user to give my application access to twitch TV (This works as expected):
[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);
}
This is the part that is not working correctly and is giving the 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();
}
Any help would be greatly appreciated. The overall end goal is to get the "access_token" so I can use it to get the current user's twitch username and be able to grab that user's channels and feeds.
I am not very good with this, but i think the problem is that you are trying to connect to localhost wich is your own computer trough a server port. if this is not the problem and this is what you want. did you think about port forwarding?