OAuth token missing when getting followed streams

I am trying to get my list of followed streams. I used to use this url:

https://api.twitch.tv/helix/users/follows?from_id=<from_id>&first=100

I am now getting this error:

{
    "error": "Unauthorized",
    "status": 401,
    "message": "OAuth token is missing"
}

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”

https://id.twitch.tv/oauth2/token?client_id=<my client id>&client_secret=<my client secret>&grant_type=client_credentials

I need help getting the OAuth token to pass along with my request to get my followed streams. Thanks!

Client_credentials is a POST request.

You are getting a 404 as your are GET-ing instead of POST-ing

1 Like

Barry,

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?

The response when you get the token returns an expires_in which in seconds is when the token expires

Depends on the token type, regular user tends to be four hours, implicit auth and app access 60 days

No.

App access tokens, just generate another one.
User tokens you get a refresh token you can use

1 Like

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;

    }

}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.