Issues accessing Twitch API through Python

Solution for Issues accessing Twitch API through Python
is Given Below:

I am trying to access the Twitch API to make a simple request for channel details. I am consistently being met with a 401 Unauthorised error despite feeding in the OAUTH code I am generating successfully. I am using the formatting found in the docs:

GET '' 
--header 'client-id: wbmytr93xzw8zbg0p1izqyzzc5mbiz' 
--header 'Authorization: Bearer 2gbdx6oar67tqtcmt49t3wpcgycthx'

This is what is written on the website. I have translated this into Python:

import os
import requests

URL = ""
CLIENT_ID = os.environ['CLIENT_ID']
GRANT_TYPE = "client_credentials"
  "client_id": CLIENT_ID,
  "client_secret": CLIENT_SECRET,
  "grant_type": GRANT_TYPE

r1 = = URL, params = PARAMS)

token = r1.json()["access_token"]

URL2 = ""
  "client_id": CLIENT_ID,
  "authorization": f"Bearer {token}"

r2 = requests.get(url = URL2, params = PARAMS2)

data = r2.json()

The first section of code that gets the auth token works fine, but it isn’t accepted when fed into the second half, giving the following error:

{'error': 'Unauthorized', 'status': 401, 'message': 'OAuth token is missing'}

My question is how can I get the API to accept the auth token and process my request. Thanks in advance for any help 🙂

Edit – Repl link to run/fork –

headers = {
    'Client-ID': client_id,
    'Authorization': 'Bearer ' + keys['access_token']


stream = requests.get('' + streamer_name, headers=headers)

You need to pass headers, not params.