我正在尝试从thetvdb访问API v2( api.thetvdb ).不幸的是,我总是收到403错误.
I am trying to access the API v2 from thetvdb (api.thetvdb). Unfortunately I always get the 403 error.
这就是我所拥有的:
#!/usr/bin/python3 import requests url = "api.thetvdb/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.
According to the API documentation I have to authenticate in order to get a token. But I just get 403 Forbidden.
现在我尝试使用curl
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"}' 'api.thetvdb/login'这很好用.谁能解释我所缺少的东西?这让我发疯.
And this worked perfectly. Can anyone explain me what I am missing? This is driving me insane.
我也尝试过
post = requests.post(url, data = json.dumps(payload), headers = headers)相同的错误.
推荐答案您必须explicitly将payload转换为json字符串并作为data传递.看来您已完成操作,也可以尝试将用户代理设置为curl/7.47.1
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)程序看起来像
#!/usr/bin/python3 import requests import json url = "api.thetvdb/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)更多推荐
Python请求.错误403
发布评论