从 thetvdb.com 访问 API v2 时出错 [Python 请求。错误 403]

2024-02-23

我正在尝试从以下位置访问 API v2电视数据库 https://api.thetvdb.com。不幸的是我总是收到 403 错误。

这是我所拥有的:

#!/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)

根据 API 文档,我必须进行身份验证才能获得令牌。但我刚刚得到 403 Forbidden。

现在我尝试使用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'

这非常有效。谁能解释一下我缺少什么?这让我发疯。

我也尝试过

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

同样的错误。


你必须explicitly转换payload到 json 字符串并传递为data。看来您已经完成了,您也可以尝试将用户代理设置为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)

该程序看起来像

#!/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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 thetvdb.com 访问 API v2 时出错 [Python 请求。错误 403] 的相关文章

随机推荐