Python请求.错误403 [英] Python requests. Error 403

查看:756
本文介绍了Python请求.错误403的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从thetvdb.com访问API v2( https://api.thetvdb.com ).不幸的是,我总是收到403错误.

I am trying to access the API v2 from thetvdb.com (https://api.thetvdb.com). Unfortunately I always get the 403 error.

这就是我所拥有的:

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

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"}' 
'https://api.thetvdb.com/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)

相同的错误.

推荐答案

您必须explicitlypayload转换为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 = "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)

这篇关于Python请求.错误403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆