使用 apikey 请求 JSON 输出时出现 403 Forbidden 错误
Getting 403 Forbidden error when requesting JSON output with apikey
我正在尝试使用 Python 从服务器请求信息。 API 键是正确的,但我仍然收到 403 错误。它适用于卷曲,但不适用于 Python.
这是输出 JSON:
的 curl 代码
curl -H "apiKey: xxx" https://kretaglobalmobileapi.ekreta.hu/api/v1/Institute/3928
这是我输出禁止错误的代码:
from urllib.request import Request, urlopen
import json
ker = Request('https://kretaglobalmobileapi.ekreta.hu/api/v1/Institute/3928')
ker.add_header('apiKey', 'xxxx')
content = json.loads(urlopen(ker))
print(content)
问题是什么?
urlopen 通常 returns 一个 HTTPResponse 对象。因此,为了读取内容,请使用 read() 函数。否则你的代码看起来不错。
req = Request('https://kretaglobalmobileapi.ekreta.hu/api/v1/Institute/3928')
req.add_header('apikey', 'xxx')
content = urlopen(req).read()
print(content).
如果上述方法有效,您还可以使用另一个库来请求实例,
r = requests.get('<MY_URI>', headers={'apikey': 'xxx'})
我正在尝试使用 Python 从服务器请求信息。 API 键是正确的,但我仍然收到 403 错误。它适用于卷曲,但不适用于 Python.
这是输出 JSON:
的 curl 代码curl -H "apiKey: xxx" https://kretaglobalmobileapi.ekreta.hu/api/v1/Institute/3928
这是我输出禁止错误的代码:
from urllib.request import Request, urlopen
import json
ker = Request('https://kretaglobalmobileapi.ekreta.hu/api/v1/Institute/3928')
ker.add_header('apiKey', 'xxxx')
content = json.loads(urlopen(ker))
print(content)
问题是什么?
urlopen 通常 returns 一个 HTTPResponse 对象。因此,为了读取内容,请使用 read() 函数。否则你的代码看起来不错。
req = Request('https://kretaglobalmobileapi.ekreta.hu/api/v1/Institute/3928')
req.add_header('apikey', 'xxx')
content = urlopen(req).read()
print(content).
如果上述方法有效,您还可以使用另一个库来请求实例,
r = requests.get('<MY_URI>', headers={'apikey': 'xxx'})