由于401,API请求失败

时间:2019-03-29 19:30:41

标签: python urllib

我试图通过使用API​​来改善我玩游戏时的体验,从而学习如何使用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

所以我不太确定这里出了什么问题,对您的帮助将不胜感激!

0 个答案:

没有答案