Using curl to set game category using Channel Update

I want to automate the changing of Twitch game category because it’s tedious having to click and type in a lookup field every time I change games, and I change games at least 2 or 3 times every stream. I’m sure this is a common nuisance for streamers.

It looks like I could probably automate this using OBS, upon scene change call curl to access the Twitch API via a batch script of some kind.

I looked up previous answers on this topic, but I’m confused about how curl is used to authenticate to get a token, so it can then do the PUT request to change channel game property.

I found the reference here, and I registered an application to get a Client ID to use.

curl -H 'Client-ID: XXXXXX' \
-H 'Accept: application/vnd.twitchtv.v5+json' \
-H 'Authorization: OAuth YYYYYY' \ -d 'channel[game]=Mordhau' \
-X PUT 'https://api.twitch.tv/kraken/channels/ZZZZZZ'

If I simplify the example in the documentation to just set the game category, it would look similar to above. Though I don’t understand how I get the OAuth token.

XXXXXX - Client ID, I registered an application, and I used http://localhost as the endpoint URL which is hopefully OK for this purpose?

YYYYYY - I couldn’t get my head around the OAuth documentation. Maybe I need to do an authentication request to get an OAuth token?

ZZZZZZ - For the channel ID is it OK to use the twitch username? Or would I need to do another GET to look up my channel ID first, and plug that into my statements?

I’m using Windows 64bit curl in case that has any impact on the command syntax. If someone familiar with curl and the twitch API could comment an example of how to authenticate, and update the game category it would be of much help.

If the above is not possible or suitable, my suggestion would be at the least, Twitch game category dialog should show recently used game categories for the user, or allow them to set a list of common categories to choose from. Having to type and search each time is annoying enough it drove me here :slight_smile:

Using localhost is fine, as long as you have either a server running on localhost to handle the OAuth process, or manually do it from the URL bar of the browser.

You need the broadcaster of the channel you’re wishing to edit go through the OAuth flow https://dev.twitch.tv/docs/authentication/getting-tokens-oauth#oauth-authorization-code-flow to grant you permissions to edit the game that they are playing.

No, a username will not work here. As the documentation specifies, https://dev.twitch.tv/docs/v5/reference/channels#update-channel, you need to use the channels Twitch ID.

Thank you for the prompt reply and detailed response. It seems that OAuth is a bit more complicated than I hoped, to fully automate this from a batch script would require also parsing the authentication response to get the token to use in the PUT request… not as simple as stacking up a series of requests with predefined values like I was hoping to script.

Probably someone more familiar with OAuth and Web APIs could throw something together in a pinch, but for me it’s beyond my experience level for something I could do in a short amount of time.

May be time to stump up and buy a stream deck. Really it’s something OBS should inherently provide within it’s own Twitch integration, which already authenticates using OAuth, though that’s a feature request for the OBS team really.

Thanks for your help

It is worth noting, you only need to do the “complicated bit” once.

As that gives you the token (and it’s refresh token), and then you can use that token till it expires, then refresh the token for a new token.

OAuth is a complicated topic to wrap your head around when you do attempt it for the first time, but i can recommend digging into it deeper if you plan to ever do scripting with any API that requires authentication later on.

The general Idea of OAuth is that an “application” (in this case, you or your script) is granted access to certain elements of someone’s account or to execute actions for someone. These actions or pieces of info are controlled by “scopes”, basically telling the first party (Twitch, in this case) “Hey, i want to be able to access the following resources for this user”.

This then prompts the user (usually by opening a URL in their Browser) to log in or otherwise verify their identity so they can confirm that the application may request the mentioned scopes.

Depending on the selected authorization flow, the user then gets redirected to your redirect URL - What Dist linked uses the Authorization Code flow, so a code (NOT an Oauth token!) is passed along with the URL the user is redirected to. This code then needs to be exchanged for a token.

This is then done as described in the docs by sending a POST request to https://id.twitch.tv/oauth2/token with the appropriate parameters. In the response to this request, you will finally get the OAuth token that you can use for the above mentioned YYYYYY.

Specially to note is how long the token is valid - you’ll have to effectively repeat this procedure once the token expires. You could also refresh it as mentioned here.

Hope this helps a bit more to understand the topic!

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