Twitch api get latest follower

83 views Asked by At

I would like to get my latest follower using python from the Twitch API. Sadly i don`t know how to work with webhooks so i hoped I can return the latest follower using this script:

`import requests
import json

### ⬇️ ENTER YOUR INFO HERE ⬇️ ###
client_id = "xxxxxx"
client_secret = "xxxxxxxxx"
username = 'xxxxx'

# Get OAuth token
def get_oauth_token(client_id, client_secret):
    url = 'https://id.twitch.tv/oauth2/token'
    params = {
        'client_id': client_id,
        'client_secret': client_secret,
        'grant_type': 'client_credentials'
    }
    response = requests.post(url, params=params)
    data = response.json()
    return data.get('access_token')

# Get user ID
def get_user_id(username, oauth_token):
    url = f'https://api.twitch.tv/helix/users?login={username}'
    headers = {
        'Client-ID': client_id,
        'Authorization': f'Bearer {oauth_token}'
    }
    response = requests.get(url, headers=headers)
    data = response.json()
    return data['data'][0]['id'] 

# Get latest follower
def get_latest_follower(user_id, oauth_token):
    url = f'https://api.twitch.tv/helix/channels/followers?broadcaster_id={user_id}'
    headers = {
        'Client-ID': client_id,
        'Authorization': f'Bearer {oauth_token}'
    }
    response = requests.get(url, headers=headers)
    data = response.json()
    return data['data'][0]['user_name'] if data.get('data') else None

# Main function
def main():
    oauth_token = get_oauth_token(client_id, client_secret)
    if oauth_token:
        user_id = get_user_id(username, oauth_token)
        if user_id:
            latest_follower = get_latest_follower(user_id, oauth_token)
            if latest_follower:
                print("Your latest follower on Twitch is:", latest_follower)
            else:
                print("You don't have any followers yet on Twitch.")
        else:
            print("Failed to retrieve user ID.")
    else:
        print("Failed to retrieve OAuth token.")

if __name__ == "__main__":
    main()

`

All i get from the api call is the number of followers (By the way, I created an aplication in twitch dev dasboard and I`m trying to access my own channel using the credentials i got from the dashboard (App_id and app_secret).

Why would twitch not let me view my own followers? What am I doing wrong?

0

There are 0 answers