Python API请求因错误而失败

Python API请求因错误而失败,python,urllib,Python,Urllib,我正在尝试学习如何使用API,我想我会用它来提高我玩游戏的体验。我想做的是从API获取我的统计数据开始,我使用以下代码: import ssl from urllib.request import Request, urlopen ssl._create_default_https_context = ssl._create_unverified_context key = input("what is your API key?") req = Request('https://publi

我正在尝试学习如何使用API,我想我会用它来提高我玩游戏的体验。我想做的是从API获取我的统计数据开始,我使用以下代码:

import ssl
from urllib.request import Request, urlopen

ssl._create_default_https_context = ssl._create_unverified_context

key = input("what is your API key?")
req = Request('https://public-api.tracker.gg/apex/v1/standard/profile/1/daanenjasper')
#line below added to avoid error 403
req.add_header('User-agent', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
req.add_header('apikey', key)
content = urlopen(req).read()

print(content)
这将导致以下输出,其中我只能够对http error 401进行十进制运算,但这并没有多大帮助

Traceback (most recent call last):
  File "/Users/daankoning/Documents/projects/apex/main.py", line 11, in <module>
    content = urlopen(req).read()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 532, in open
    response = meth(req, response)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 570, in error
    return self._call_chain(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Unauthorized
回溯(最近一次呼叫最后一次):
文件“/Users/daanking/Documents/projects/apex/main.py”,第11行,在
content=urlopen(req).read()
urlopen中的文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第223行
返回opener.open(url、数据、超时)
打开文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第532行
响应=方法(请求,响应)
http_响应中的文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第642行
“http”、请求、响应、代码、消息、hdrs)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第570行出错
返回自我。调用链(*args)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第504行,在调用链中
结果=func(*args)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py”,第650行,默认为http\u error\u
raise HTTPError(请求完整的url、代码、消息、hdrs、fp)
urllib.error.HTTPError:HTTP错误401:未经授权

所以我真的不确定这里出了什么问题,任何帮助都将非常感谢

我建议您尝试使用requests包…它比任何一个urllib版本都更易于使用。这在很大程度上取决于您调用的API期望API键的格式,它可能只是期望API键是Base64编码的,或者您的头可能是错误的;尝试类似于
'Authorization':'Bearer{}.format(apikey)
'Authorization':'Basic{}.format(apikey)
。我同意Chris的观点,但如果您只是想学习基本知识,请尽可能使用
requests
模块,这是一个行业标准(除非出于某种原因不能使用外部lib,否则您几乎不会看到urllib),而且更容易推理,更友好,等等,我建议您尝试使用requests包…它比任何一个urllib版本都更容易使用。这里的很多内容取决于您调用的API期望API键的格式,它可能只是期望API键是Base64编码的,或者您的头可能是错误的;尝试类似于
'Authorization':'Bearer{}.format(apikey)
'Authorization':'Basic{}.format(apikey)
。我同意Chris的观点,如果你只是想学习基本知识,那么尽可能使用
请求
模块,这是一个行业标准(除非你出于某种原因不能使用外部lib,否则你几乎看不到urllib),而且更容易推理,更友好,等等。