Search code examples
pythonpython-3.xapipython-requeststwitch

Python Twitch API Attribute Error problem


I received the error: AttributeError: 'set' object has no attribute 'items' while trying to get info via twitch API.

import requests

ENDPOINT = 'https://api.twitch.tv/kraken/clips/top?channel=Twitch&period=month&trending=true&limit=1'

HEAD = {
    'Accept: application/vnd.twitchtv.v5+json',
    'Client-ID: HIDDEN',
}

response = requests.get(url=ENDPOINT, headers=HEAD)
print (response.text)

Solution

  • The problem is the way you have defined your header in HEAD, you have actually created a set, not a dictionary. Revise your HEAD as follows:

    HEAD = {
        'Accept': 'application/vnd.twitchtv.v5+json',
        'Client-ID': 'HIDDEN',
    }
    

    Not that the key and value are wrapped inside a quotation.

    Dictionary:

    {'key', 'value'}
    

    What you had done was to create a Set:

    {'key value'}