Modified code
import requests
from urllib.parse import urlencode
client_id = '***'
client_secret = '***'
data = {
'client_id': client_id,
'redirect_uri': 'http://localhost',
'response_type': 'code',
'scope': 'channel:manage:broadcast'
}
url = 'https://id.twitch.tv/oauth2/authorize?' + urlencode(data, doseq=True)
print('Authorization URL:')
print(url)
code = input('ENTER CODE: ')
assert len(code) == 30
# auth
data = {
'client_id': client_id,
'client_secret': client_secret,
'code': code,
'grant_type': 'authorization_code',
'redirect_uri': 'http://localhost'
}
response = requests.post('https://id.twitch.tv/oauth2/token', data=data).json()
print(response)
token = response['access_token']
headers = {
'Client-Id': client_id,
'Authorization': 'Bearer ' + token,
}
# get broadcaster id
data = {
'login': ['Flexlolo']
}
url = 'https://api.twitch.tv/helix/users?' + urlencode(data, doseq=True)
response = requests.get(url, headers=headers).json()
print(response)
broadcaster_id = response['data'][0]
# change title
data = {
'broadcaster_id': broadcaster_id,
'title': 'NEW TITLE STRING'
}
url = 'https://api.twitch.tv/helix/channels?' + urlencode(data, doseq=True)
response = requests.patch(url, headers=headers).json()
print(response)
output of the script
Authorization URL:
https://id.twitch.tv/oauth2/authorize?client_id=***&redirect_uri=http%3A%2F%2Flocalhost&response_type=code&scope=channel%3Amanage%3Abroadcast
ENTER CODE: ***
{'access_token': '***', 'expires_in': 15181, 'refresh_token': '***', 'scope': ['channel:manage:broadcast'], 'token_type': 'bearer'}
{'data': [{'id': '89346924', 'login': 'flexlolo', 'display_name': 'Flexlolo', 'type': '', 'broadcaster_type': '', 'description': '', 'profile_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/77a99df0-48fc-4764-8d0b-29f6da6df736-profile_image-300x300.png', 'offline_image_url': '', 'view_count': 4972, 'created_at': '2015-04-24T07:05:45.847672Z'}]}
{'error': 'Unauthorized', 'status': 401, 'message': 'incorrect user authorization'}