将Fortnite Tracker api与urllib.reques

2024-09-30 14:35:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从Fortnite tracker api获取用户统计信息。在

我有一个api密钥,并使用正确的url作为indicated in the documentation

模板url:

https://api.fortnitetracker.com/v1/profile/{platform}/{epic-nickname}

所需的url:

^{pr2}$

如果我在浏览器中使用这个链接,我得到{"message":"No API key found in request"}(因为我没有传递API密钥),所以链接应该是正确的。另外,如果我没有通过urllib传递api密钥,那么仍然会得到403错误。在

我已经了解了如何在请求中传递头:How do I set headers using python's urllib?

到目前为止,有这样的代码:

import urllib.request as ur

request = ur.Request('https://api.fortnitetracker.com/v1/profile/pc/xantium0', headers={'TRN-Api-Key' : 'xxx'})

response = ur.urlopen(request)

print(response.read())

运行时出现以下错误:

urllib.error.HTTPError: HTTP Error 403: Forbidden

403签出身份:

HTTP 403 is a standard HTTP status code communicated to clients by an HTTP server to indicate that the server understood the request, but will not fulfill it. There are a number of sub-status error codes that provide a more specific reason for responding with the 403 status code.

https://en.wikipedia.org/wiki/HTTP_403

如果不在头中传递api键,那么响应也是一样的。在

我只能想到三个原因,这个代码不能工作:

  • 我传递了错误的头名称(即它不是TRN-Api-Key
  • 我的代码不正确,实际上我没有向服务器传递头
  • 我被禁止了

我的问题是我认为我的代码是正确的:

documentation

urllib.request.Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None)

我已经通过了url,我已经通过了headers(不要与data争论混淆)。api文档还提到应该在头中传递它。在

我也很确定我需要使用TRN-Api-Key,正如api文档中所示:

TRN-Api-Key: xxx

同样在this question(使用Ruby)中:

header = {
    key: "TRN-Api-Key: Somelong-api-key-here"
}

或者我被禁止了(这是可能的,虽然我15分钟前拿到了钥匙),有没有办法检查?是否会返回此错误?在

是什么阻止我获取用户统计信息?在


Tags: thekey代码httpsapihttpurlrequest
1条回答
网友
1楼 · 发布于 2024-09-30 14:35:30

尝试使用requests,一个Python式的、快速的、广泛使用的模块。在

import requests


url = 'https://api.fortnitetracker.com/v1/profile/pc/xantium0'
headers = {
    'TRN-Api-Key' : 'xxx'
}

response = requests(url, headers=headers)

print('Requests was successful:', response.ok)
print(response.text)

如果不起作用,您可以使用浏览器访问url,然后检查请求: 在Firefox中,按Cntrl+Shift+E,在Chrome Cntrl+E中(或使用Cntrl+Shift+I检查,然后转到网络)。按“https://api.fortnitetracker.com/v1/profile/pc/xantium0”并更改标题。在Firefox上有修改和重新发送按钮。检查响应,并最终尝试更改头api密钥名。在

希望这有帮助,让我知道。在

相关问题 更多 >