Hey,
New version:
The new version can now be found on github GitHub - wuestenarchitekten/Twitcher: a component to communicate with twitch with instructions here: https://derivative.ca/community-post/asset/twitcher-twitchwatching-everyone
this little component let’s you connect to the Twitch API v5 as well as the PubSub API to subscribe to topics such as when users “cheer” a channel, somebody subscribes to a channel, a purchase is made on the channel or somebody “whispers” your twitch account.
The underlying network consists of the webCOMP from the palette as well as a websocket DAT to connect to the PubSub interface.
To get started you need to:
- register an App on dev.twitch.tv
- create a callback page on a webserver you own
- retrieve an OAuth token
- start and configure the attached component
Register an App on dev.twitch.com
with a twitch account already registered go to dev.twitch.tv/dashboard. Here select “Apps” and click on “Register Your Application”.
Fill out the required fields and note the OAuth Redirect URL. This will be the place where you will retrieve your OAuth Token. Click the “Create” button.
create a callback page on a webserver you own
At the location you have specified for your OAuth callback URL create a page. The page needs to link to the id.twitch.tv/oauth2/authorize endpoint with parameters passed being the
- redirect_url (exactly as you entered it in the App Create Page)
- client_id (you can retrieve this from your Manage Application Page on twitch)
- the scope strings which is a list of data points you want to have access to from your twitch component. A full list of available scopes and what access they will allow you can be found here: dev.twitch.tv/docs/authentication/#scopes
<a href=https://id.twitch.tv/oauth2/authorize?client_id=mwh9kz5xq550ojd9pf2mkbxxjpq29w&redirect_uri=https://www.webriot.de/twitch/&response_type=token&scope=chat:read%20channel:moderate%20chat:edit%20whispers:read%20whispers:edit%20channel_commercial%20channel_editor%20channel_feed_edit%20channel_feed_read%20channel_read%20channel_subscriptions%20viewing_activity_read>Click me for access</a>
retrieve the OAuth Token
Navigate in your browser to the callback page where you created the link shown above. Click onthe link and you should be asked to either login to your Twitch Account, or if you are already logged in, it will ask you if you want to give all the permissions to the App that you have been requesting via the scope parameter passed along in the link.
You should be redirected to the page with the link. Here, copy the url and search for the “access_token” parameter. An example url would look like this:
https://www.webriot.de/twitch/#access_token=4eg3t4by8z2jq4pnjt8ekaud9sx8i3&scope=chat%3Aread+channel%3Amoderate+chat%3Aedit+whispers%3Aread+whispers%3Aedit+channel_commercial+channel_editor+channel_feed_edit+channel_feed_read+channel_read+channel_subscriptions+viewing_activity_read&token_type=bearer
copy the access token.
start and configure the twitch component
Start the attached file and copy Client ID as well as OAuth Token into the parameters on the Twitch parameter page.
============================================================================
NOTE: KEEP THAT OAuth TOKEN A SECRET! depending on what rights you have given the application, anyone with the OAuth token now can make use of the API to control the account it is associated with. You can either recreate a token to invalidate the previous one or make sure to delete it from your file before passing it on to others or showing the parameters of the Twitch Component on a Twitch Stream!
==============================================================================
Switch over to the PubSub parameter page and click the “Get ChannelID” Pulse parameter.
If a ChannelID is being displayed in the ChannelID Parameter, you are now ready to start listening to Bits, Channel Subscriptions, Commerce Events and Whispers.
Any event is being passed into the callback DAT specified under the Callbacks parameter. By default the received json is printed to the textport. By editing the Callbacks DAT you can control what should be happening on such an event.
For example a whisper event will return a python dictionary similar to this:
{
'thread_id': '104027510_114993034',
'tags': {
'badges': [],
'login': 'touchdesigner',
'emotes': [],
'user_type': '',
'color': '',
'display_name': 'touchdesigner'
},
'recipient': {
'profile_image': None,
'badges': [],
'display_name': 'Wuestenarchitekten',
'user_type': '',
'id': 104027510,
'color': '',
'username': 'wuestenarchitekten'
},
'body': 'Hello Wuestenarchitekten!!',
'id': 26,
'from_id': 114993034,
'sent_ts': 1543873537,
'nonce': '2af192ba897205942937d9cf7e14877f',
'message_id': '6ce98ed0-a944-44e1-9a31-204a6c262d32'
}
An overview over all PubSub topics can be found here: dev.twitch.tv/docs/pubsub/
Access to the Twitch API v5
You can use the included webCOMP to access the Twitch API v5 dev.twitch.tv/docs/v5/
For example to get more information about your channel use following python code:
# specify the path of the Twitch Component
twitchCOMP = op('twitch')
url = 'https://api.twitch.tv/kraken/channel'
requestType = 'get'
header = {
'Accept': 'application/vnd.twitchtv.v5+json',
'Client-ID': twitchCOMP.par.Clientid.eval(),
'Authorization': 'OAuth {0}'.format(twitchCOMP.par.Oauthtoken.eval())
}
twitchCOMP.Send(url=url, requestType=requestType, header=header)
The response from the Twitch server will be processed by a DAT called “webCOMPCallback” inside the Twitch Component. In it’s included “receive” function is were you would have to parse the responses.