I have tried reading the documentation for the new API but I am not sure how exactly I get an OAuth token. I have tried “refreshing” the token using this call but I am getting the error “404 page not found”
Thank you so much. What a dumb mistake to get held up on. I got that working and I got my calls working. The access_token says it will expire. What is the default time on the expiration? Is there a way to increase the expiration time?
Thanks again for all the info. Here is a quick solution I have for the new process in c# incase anyone comes across this post and needs help. I stored it in a cookie to be able to be able to pull the token and validate it. If its not valid, I refresh the token. If the cookie doesn’t exist, I create the cookie and get a new token.
string validToken = getValidToken();
protected string getValidToken()
{
string token;
//Check cookie for token
HttpCookie twitchTokenCookie = Request.Cookies["TwitchToken"];
// Read the cookie information and display it.
if (twitchTokenCookie != null)
{
//get token from cookie and validate
token = twitchTokenCookie.Value;
if (isTokenValid(token))
{
return token;
}
else
{
token = getRefreshToken();
HttpCookie myCookie = new HttpCookie("TwitchToken");
DateTime now = DateTime.Now;
// Set the cookie value.
myCookie.Value = token;
// Set the cookie expiration date.
myCookie.Expires = now.AddYears(50); // For a cookie to effectively never expire
// Add the cookie.
Response.Cookies.Add(myCookie);
return token;
}
}
else
{
token = getRefreshToken();
HttpCookie myCookie = new HttpCookie("TwitchToken");
DateTime now = DateTime.Now;
// Set the cookie value.
myCookie.Value = token;
// Set the cookie expiration date.
myCookie.Expires = now.AddYears(50); // For a cookie to effectively never expire
// Add the cookie.
Response.Cookies.Add(myCookie);
return token;
}
}
protected bool isTokenValid(string token)
{
string url = "https://id.twitch.tv/oauth2/validate";
Uri address = new Uri(url);
// Create the web request
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
request.Headers["Authorization"] = "OAuth " + token;
// Set type to GET
request.Method = "GET";
request.ContentType = "text/xml";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
// Console application output
string strOutputJson = FormatJson(reader.ReadToEnd());
//dynamic jsonObj = JsonConvert.DeserializeObject(strOutputJson);
var data = JsonConvert.DeserializeObject<ValidateToken>(strOutputJson);
if (string.IsNullOrEmpty(data.client_id))
return false;
else
return true;
}
}
protected string getRefreshToken()
{
string url = "https://id.twitch.tv/oauth2/token?client_id=<Your Client ID>&client_secret=<Your Client Secret>&grant_type=client_credentials";
Uri address = new Uri(url);
// Create the web request
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
// Set type to POST
request.Method = "POST";
request.ContentType = "text/xml";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
// Console application output
string strOutputJson = FormatJson(reader.ReadToEnd());
//dynamic jsonObj = JsonConvert.DeserializeObject(strOutputJson);
var data = JsonConvert.DeserializeObject<RefreshToken>(strOutputJson);
return data.access_token;
}
}