Authorization Error with Helix

Apologies if I’m missing something obvious, but I’m trying to access the Streams API in Helix.

1 I get client-credentials through the id.twitch.tv endpoint, like so:

auth_url = "https://id.twitch.tv/oauth2/token?client_id=" +  client_id + "&client_secret=" + client_secret + "&grant_type=client_credentials"

r = requests.post(auth_url, timeout=15)

r.raise_for_status()

info = r.json()

client_token = info['access_token']

This returns the token just fine.

2 Then, I pass this on to the endpoint:

headers = {
        'Client_ID': client_id,
        'Authorization': 'Bearer ' + client_token,
}

helix_url = "https://api.twitch.tv/helix/streams?first=20"

helix_r = requests.get(helix_url, headers, timeout = 15)

helix_r.raise_for_status()

stream_info = helix_r.json()

print(stream_info)

And I get a 401 error. The message body says the “OAuth token is missing”.

What am I missing? Thanks in advance.

What language is this?

Is this Python and requests?

The quick start docs

https://requests.readthedocs.io/en/master/user/quickstart/

Says you have formatted your request wrong

helix_r = requests.get(helix_url, headers, timeout = 15)

should be

helix_r = requests.get(helix_url, headers=headers, timeout = 15)

I believe

Thanks for getting back to me. This is Python and I am using the requests library. I made the fix you suggested and I got the same error. I imagine it has something to do with encoding? I have used the same code (i.e., the call to the OAuth endpoint) to get and send the client-credentials via another API (that wraps the lower level guts) and it works. Just can’t figure out why my code isn’t working.

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