我正在尝试向 API 发出经过身份验证的 GET 请求。这是我第一次尝试使用 Python 的request
库。我已经查看了与此类似的帖子,但它们似乎太笼统而无法回答我的问题。他们的答案几乎适用于我处理过的所有其他案例,所以感觉有点卡住了。
请求头相当长:
':authority': 'api-WEBSITE.com',
':method': 'GET',
':path': 'ENDPOINT',
':scheme': 'https',
'accept': 'application/json',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
'authorization': 'AUTH_TOKEN',
'content-type': 'application/json',
'cookie': 'VERY_LONG_COOKIE',
'origin': 'https://WEBSITE.com',
'referer': 'https://WEBSITE.com/app',
'user-agent': 'LIST_OF_BROWSERS'
我提出这个请求的代码:
import requests
requestURL = "https://api-WEBSITE.com/ENDPOINT"
parameters = {
':authority': 'api-WEBSITE.com',
':method': 'GET',
':path': 'ENDPOINT',
':scheme': 'https',
'accept': 'application/json',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
'authorization': 'AUTH_TOKEN',
'content-type': 'application/json',
'cookie': 'VERY_LONG_COOKIE',
'origin': 'https://WEBSITE.com',
'referer': 'https://WEBSITE.com/app',
'user-agent': 'LIST_OF_BROWSERS'
}
response = requests.get(requestURL, parameters)
print(response.status_code)
当我运行它时,我收到一个 401 状态代码,要求进行身份验证;但是,我似乎无法找出引发此 401 错误的原因。
要为 python 请求提供标头:你必须这样做 r = requests.get(url, headers = headersDict)
其中 headersDict 是您要添加到请求中的标头的有效字典
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句