1

In the last weeks, Twitch began sending ads that streamlink couldn't block anymore. One of the solutions proposed by streamlink is to get the auth-token after a sucessuful login through your browser to authenticate. By doing this, if you have Twitch Turbo or you are a sub of some channel, you wont get the ads.

In this post and, later explained in this one, they showed how you can get the auth-token to use the commands --http-header or --twitch-api-header in the streamlink CLI.

But, I'm using the streamlink python package to build a GUI and I don't know how can I use the auth-token to authenticate through there.

Here's what I have tried already:

class MyClass:
    def __init__(self):
        auth = 'abcdefghijklmnopqrstuvwxyz0123'
        self.session = streamlink.Streamlink(options={'--twitch-api-header': f"Authorization=OAuth {auth}"})

But the Streamlink session class can't handle that key.

Can anyone help me? Thank you.

NeoFahrenheit
  • 347
  • 5
  • 16
  • [Show](https://stackoverflow.com/help/minimal-reproducible-example) us the code you've written so far. – J_H Dec 09 '22 at 20:51
  • @J_H Done. It's there! – NeoFahrenheit Dec 09 '22 at 21:03
  • You wrote `'--twitch-api-header'`. Are you _sure_ the leading double dash is part of what the documentation asks for? (I didn't notice anything saying one way or the other in the cited URL, it just seems an unusual spelling.) – J_H Dec 09 '22 at 21:19
  • Yes, but that's for the CLI version. I can't figure it out how to pass to the streamlink python package. – NeoFahrenheit Dec 09 '22 at 22:01

1 Answers1

2

Pretty old question but this should help.

session = streamlink.Streamlink()
session.set_option('http-headers', {'Authorization': 'OAuth XXX'})
# didn't see the --twitch-api-header in .set_option, http-headers was ok
stream = session.streams('https://www.twitch.tv/forsen')

From Streamlink Ref: https://streamlink.github.io/cli/plugins/twitch.html

In order to get the personal OAuth token from Twitch's website which identifies your account, open Twitch.tv in your web browser and after a successful login, open the developer tools by pressing F12 or CTRL+SHIFT+I. Then navigate to the "Console" tab or its equivalent of your web browser and execute the following JavaScript snippet, which reads the value of the auth-token cookie, if it exists:

document.cookie.split("; ").find(item=>item.startsWith("auth-token="))?.split("=")[1]

Copy the resulting string consisting of 30 alphanumerical characters without any quotations.

The final Authorization header which will identify your account while requesting a streaming access token can then be set via Streamlink's --http-header or --twitch-api-header CLI arguments. The former will set the header on any HTTP request made by Streamlink, even HLS Streams, while the latter will only do that on Twitch API requests, which is what should be done when authenticating and which is the reason why this CLI argument was added.

The value of the Authorization header must be in the format of OAuth YOUR_TOKEN. Notice the space character in the argument value, which requires quotation on command line shells:

streamlink "--twitch-api-header=Authorization=OAuth abcdefghijklmnopqrstuvwxyz0123" twitch.tv/CHANNEL best

The entire argument can optionally be added to Streamlink's (Twitch plugin specific) config file, which doesn't require quotes:

twitch-api-header=Authorization=OAuth abcdefghijklmnopqrstuvwxyz0123
3V1LXD
  • 55
  • 1
  • 10
Prigless
  • 21
  • 1
  • 5
  • Hi Prigless. welcome to Stack Overflow and thank you for your answer! While this code may be useful, the community would appreciate if you would improve your answer by saying why it works, how it works, when it should be used, and what its limitations are. Please edit your answer to include explanation and link to relevant documentation. See [Explaining entirely code-based answers](https://meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers) for more details. – mingaleg Feb 07 '23 at 16:50