Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while accessing API v2 from thetvdb.com [Python requests. Error 403 ]

Tags:

I am trying to access the API v2 from thetvdb.com. Unfortunately I always get the 403 error.

Here is what I have:

#!/usr/bin/python3
import requests

url = "https://api.thetvdb.com/login"
headers = {'content-type': 'application/json'}
payload = {"apikey":"123","username":"secretusername","userkey":"123"}
post = requests.post(url, data = payload, headers = headers)
print(post.status_code, post.reason)

According to the API documentation I have to authenticate in order to get a token. But I just get 403 Forbidden.

Now I tried it using curl:

curl -X POST --header 'Content-Type: application/json' --header 'Accept: 
application/json' -d  
{"apikey":"123","username":"secretusername","userkey":"123"}' 
'https://api.thetvdb.com/login'

And this worked perfectly. Can anyone explain me what I am missing? This is driving me insane.

I also tried it with

post = requests.post(url, data = json.dumps(payload), headers = headers)

Same error.

like image 763
zappendappen Avatar asked Dec 28 '16 11:12

zappendappen


1 Answers

You have to explicitly convert the payload to json string and pass asdata . It looks like you have done that also you may try setting the user-agent as curl/7.47.1

headers = {'content-type': 'application/json', 'User-Agent': 'curl/7.47.1'}
post = requests.post(url, data = json.dumps(payload), headers = headers)

The program will look like

#!/usr/bin/python3
import requests
import json    

url = "https://api.thetvdb.com/login"
headers = {'content-type': 'application/json', 'User-Agent': 'curl/7.47.1'}
payload = {"apikey":"123","username":"secretusername","userkey":"123"}
post = requests.post(url, data = json.dumps(payload), headers = headers)
print(post.status_code, post.reason)
like image 178
Sarath Sadasivan Pillai Avatar answered Sep 25 '22 16:09

Sarath Sadasivan Pillai