Search code examples
pythonpython-requeststwitch

Problem with IGDB API getting game info from url


I've been trying out the IGDB API with python only just recently but I've run into an issue with getting game info. In many cases I only have the IGDB url available for what I'm trying to do, and not the game id, but I can only seem to retrieve info based on the id.

I've tried passing both the url and the slug as parameters, but both give me a syntax error. Here's my code:

token = get_token()

client_id = "my id"

headers = {"Client-ID":client_id, "Authorization":"Bearer " + token['access_token'], "Content-Type":"application/x-www-form-urlencoded"}

# slug = url.split("/games/")[1]
# data = "fields *, cover.url, videos.video_id, release_dates.human; where slug = '"+slug+"';"
data = "fields *, cover.url, videos.video_id, release_dates.human; where url = '"+url+"';"

response = requests.post("https://api.igdb.com/v4/games", headers=headers, data=data)

game_info = json.loads(response.content.decode('utf-8'))

The response returns a 400 code, and here's the error content it gives in game_info:

[{'title': 'Syntax Error', 'status': 400, 'cause': "Expecting a STRING as input, surround your input with quotes starting at 'https' expecting {'{', 'f', '(', '[', 'true', 't', 'false', 'null', 'n'"}]

The same code works when I use where id = my_game_id. I mainly just need a way to retrieve game info using either the url or slug though, so I'm not sure what I'm missing here.


Solution

  • You must use double quotes. For example:

    data = 'fields name; where slug = "'+slug+'";'