如何为经过身份验证的API GET请求设置请求头



我正在尝试向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=请求.get(url,headers=headersDict(

其中headersDict是要添加到请求的标头的有效字典