0

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

iskra
  • 11
  • 3
  • Does this answer your question? [Using headers with the Python requests library's get method](https://stackoverflow.com/questions/6260457/using-headers-with-the-python-requests-librarys-get-method) – SuperStormer Jul 16 '22 at 19:27
  • 1
    you send `headers` in URL but you have to send it as `headers=headers` – furas Jul 16 '22 at 20:51
  • 1
    when you use mdoule `requests` then you can use `json=body` instead of `data=json.dumps(body)` – furas Jul 16 '22 at 20:52

1 Answers1

0

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.

furas
  • 134,197
  • 12
  • 106
  • 148