【问题标题】:Error while accessing API v2 from thetvdb.com [Python requests. Error 403 ]从 thetvdb.com 访问 API v2 时出错 [Python 请求。错误 403]
【发布时间】:2017-05-12 16:49:32
【问题描述】:

我正在尝试从 thetvdb.com 访问 API v2。不幸的是,我总是收到 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)

同样的错误。

【问题讨论】:

  • 尝试用 http://requestb.in URL 替换 url 并执行 python 和 curl request 。然后在浏览器中你可以看到你的请求有什么不同在这里是你可以如何使用https://exotel.in/blog/engineering/debugging-your-exotel-webhooks-more-efficiently-using-requestbin/

标签: python post python-requests


【解决方案1】:

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

【讨论】:

  • @Andersson 如果 curl 工作,这必须工作。服务器可能正在做的唯一可能检查是它可能会检查“UserAgent”
  • 成功了。感谢@Sarathsp 和 requestb.in,我发现 Python 和 Curl 之间的唯一区别是用户代理。我将其更改为 curl 并且有效。
  • @Sarathsp 我做到了。 “感谢反馈!声望低于 15 人的投票会被记录下来,但不要更改公开显示的帖子得分。” :-)
【解决方案2】:

我认为您需要在 python 请求中传递 Accept 标头。像这样的:

header = {
        'Accept' : 'application/json', 
        'Content-Type' : 'application/json'
        "Accept-Encoding": "gzip, deflate, sdch, br",
        "Accept-Language": "en-US,en;q=0.8",
        "User-Agent": "some user-agent",
    }

【讨论】:

    【解决方案3】:
        url = 'http://169.254.169.254/latest/meta-data/iam/'
        payload = {}
        headers = {'content-type': 'application/json', 'User-Agent': 'curl/7.47.1'}
       #headers = {'content-type': 'application/json'}
       response = requests.post(url, headers=headers, data=payload, 
       verify='/installed/aws/usr/lib/python2.7/site- 
       packages/certifi/cacert.pem', timeout=5)
       instance_profile_role_name = response.text
       print response.text`
    

    【讨论】:

    • 我看到了类似的问题,我尝试使用代理,但它无法解决问题。所以基本上我是aws并试图做一个卷曲来读取元数据,效果很好。但是,当我尝试使用 requests.post 时,它给了我: (403, 'Forbidden')。这是我的代码。 @Sarath Sadasivan Pillai 有什么建议吗?
    • 这是一个新问题,而不是另一个答案。它和原来的问题一样,就是payload没有先转换成json字符串。
    • 如果您有新问题,请点击 按钮提出问题。如果有助于提供上下文,请包含指向此问题的链接。 - From Review
    猜你喜欢
    • 2014-01-31
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    相关资源
    最近更新 更多