Changing stream title and game name (javascript)

Hi there,

I am trying to use ajax to change the title of a stream but I keep getting the 500 error:

Object {error: “Internal Server Error”, status: 500, message: null}

here is the code, I have tried so many variations on it - anyone got any idea what’s wrong? Is this the correct way to do it? I used this from here: http://stackoverflow.com/questions/21231590/twitch-api-changing-channel-status-fail-via-jquery-ajax-put-method

function changeTitle() { //Changing stream title 
    titleStr = "test title"
    token = userLoggedIn

    $.ajax({
                url: 'https://api.twitch.tv/kraken/channels/mychannel?channel[status]='+titleStr+'&oauth_token=' +token+'&_method=put',
                type: 'PUT',
                contentType: 'application/json',
                dataType: 'jsonp',
                success: function(data) {
                  console.log(data.status);
                  console.log(data)
                }
     	}); 
}

I don’t think that will work with JSONP. You might have to add _method=PUT as a query string param instead.

Have you tried authenticating by passing the oauth token as a header, not query param, as stated in the documentation?

curl -H 'Accept: application/vnd.twitchtv.v2+json' -H 'Authorization: OAuth <access_token>' -d "channel[status]=Playing+cool+new+game!&channel[game]=Diablo" -X PUT https://api.twitch.tv/kraken/channels/test_user1

You can use headers{} field in jQuery to send custom headers.

He already did add it in the query string, might want to change type: PUT to type: GET though.

I have tried GET and PUT and it doesn’t work still for some reason … could you give an example of using the headers? Thanks

Try

    $.ajax({
            url: 'https://api.twitch.tv/kraken/channels/mychannel?channel[status]='+titleStr+'&_method=put',
            type: 'GET',
            contentType: 'application/json',
            dataType: 'jsonp',
            headers: {
                  'Authorization': 'OAuth TOKEN_HERE' // the "OAuth " is important too!
            },
            success: function(data) {
              console.log(data.status);
              console.log(data)
            }
 	});

Of course you ARE replacing “mychannel” with your channel name? :wink:

hmmm thanks but I tried that and it still doesn’t seem to be working? aha yea I have changed it to my channel to test (AidanDaniel97)

 $.ajax({
        url: 'https://api.twitch.tv/kraken/channels/aidandaniel97?channel[status]='+titleStr+'&_method=put',
        type: 'GET',
        contentType: 'application/json',
        dataType: 'jsonp',
         headers: {
              'Authorization': token // the "OAuth " is important too!
        },
        success: function(data) {
          console.log(data.status);
          console.log(data)
        }
}); 

then I get this error:

Object {error: “Unauthorized”, status: 401, message: “Token invalid or missing required scope”}

I have also found this:

but that does not work at all, the code still gives the 500 error :confused:

Ok, I believe there must be something wrong with your access token then. I’ve just tested the following code and it works fine:

                 $.ajax({
                    url: 'https://api.twitch.tv/kraken/channels/btm_pl?channel[status]=TESTING_STUFF&_method=put&oauth_token=9c66pnqc474s87yx2cqjs7imbzrAAAA',
                    type: 'GET',
                    contentType: 'application/json',
                    dataType: 'jsonp',
                    success: function(data) {
                      console.log(data.status);
                      console.log(data)
                    }
            });

Make sure you request the channel_editor scope when asking for oauth token. Remember that asking for a different oauth token (with different scopes) further in the app flow will invalidate the previous token.

1 Like

Wooop woop!!! thanks a lot!

It seemed that I was actually being a huge huge noob and I was getting the token from the session storage but I wasn’t parsing it so I wasn’t actually getting the token I was just getting a huge string!!

Thanks a lot for your help !!!

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