Python请求.错误403

编程入门 行业动态 更新时间:2024-10-12 05:55:56
本文介绍了Python请求.错误403的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从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

本文发布于:2023-10-11 14:30:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1481974.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!