Reputation: 7
I am trying to find a twitch username from its ID? How do I do that?
For example, if I have the twitch user https://www.twitch.tv/user, I can open up the API https://api.ivr.fi/twitch/resolve/user, and find the users "id":"12345678". But how can I out from an user id, identify the user or username?
Upvotes: 0
Views: 3149
Reputation: 1411
You need to use the Twitch API, you can register an app in the Developer portal, then use this code to get the channel name:
import requests
import json
CHANNEL_ID = '51496027'
#https://dev.twitch.tv/console/apps/create
client_id = '**client_id_here**'
client_secret = '**client_secret_here**'
grant_type = 'client_credentials'
# Get access token using app authentication exchange
url = 'https://id.twitch.tv/oauth2/token'
payload = {
'client_id': client_id,
'client_secret': client_secret,
'grant_type': grant_type,
}
response = requests.post(url, data=payload)
access_token = json.loads(response.text)['access_token']
# Set up headers with access token for API requests
headers = {
'Authorization': f'Bearer {access_token}',
'Client-Id': client_id,
}
# Make API requests
url = f'https://api.twitch.tv/helix/channels?broadcaster_id={CHANNEL_ID}'
response = requests.get(url, headers=headers)
data = json.loads(response.text)
print(data['data'][0]['broadcaster_name'])
Upvotes: 1