iskra
iskra

Reputation: 11

Letterboxd api Bad Request (400) for no apparent reason python

I'm trying to access the Letterboxd api to get an authentification token but I keep getting Bad Request feedback. Here's my code:

import requests
import urllib
import json

headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Accept': 'application/json'
}

url = 'https://api.letterboxd.com/api/v0/auth/token'

body = {
    'grant_type': 'password',
    'username': 'myname',
    'password': 'mypassword'
}

response = requests.post(
    url+urllib.parse.urlencode(headers), data=json.dumps(body))
print('\n')
print(response.text)

Any idea what I did wrong? The response is just an HTTP document with no info besides the Error message "400 Bad Request". Heres the documentation if it helps https://api-docs.letterboxd.com/#auth

Upvotes: 0

Views: 911

Answers (1)

furas
furas

Reputation: 142909

First: I can't test if all code works.

You send headers in url but you should send it as part of headers=.

And when you use module requests then you can use json=body instead of data=json.dumps(body)

response = requests.post(url, headers=headers, json=body)

But header 'Content-Type': 'application/x-www-form-urlencoded' suggests that you will send data as form data, not json data - which needs data=body without converting to json

response = requests.post(url, headers=headers, data=body)

EDIT:

In your link to documentation I see link to python module (in part "Example implementations") - so maybe you should use it instead of writing own code because documentation mentionts something about "All API requests must be signed" (in part "Request signing") - and this may need extra code to create it.

Upvotes: 0

Related Questions