adrianschofield / twitchuptime

A simple Uptime counter for your Twitch.tv stream
2 stars 4 forks source link

Twitch uptime stopped working #4

Open kutibotond opened 4 years ago

kutibotond commented 4 years ago

Any idea why it stopped working and how to fix it? It would mean a lot.

adrianschofield commented 4 years ago

Hi, sadly Twitch have changed their authorization requirements for accessing this information and there's no way I can think of to work around it in such a simple script. Sorry about that

sc3w commented 4 years ago

You could change the authorization code to the following:

...
let client_id = null;
let client_secret = null;

function getStreamData() {
    //If you are testing this in IE you may need to uncomment the line below to allow cross site scripting
    //$.support.cors = true; 

    $.ajax({
        type: "post",
        url: "https://id.twitch.tv/oauth2/token?client_id=" + client_id + "&client_secret=" + client_secret + "&grant_type=client_credentials",
        dataType: 'json',
        headers: {},
        success: getAuthorizationDataCallback
    })

}

function getAuthorizationDataCallback(response) {
    //Using ajax here, could have used getJSON but the error handling is awful
    $.ajax({
        url: "https://api.twitch.tv/helix/streams?first=1&user_login=" + channel,
        dataType: 'json',
        headers: {
            'Client-ID': client_id,
            'Authorization': 'Bearer ' + response.access_token
        },
        success: getStreamDataCallback
    })

}
...

You also have to create an app in the Twitch API console and copy paste the client_id and client_secret into this script.

adrianschofield commented 4 years ago

Ah that is interesting, I had no idea you could do that, sadly it does mean having the client secret in the clear which would mean that other people "could" use the client for something else.

Thanks though you've given me something interesting to investigate.

Adrian

GDLynx commented 4 years ago

In your original code, the client secret is visible too, to prevent this , I used my own endpoint (which is hosted via Zeit's Now) that will make the request to Twitch's Helix API server side. See commit: use custom endpoint so client secret is not needed nor exposed