我正在尝试使用Requests进行API调用。 我们应该执行AuthenticatedSession并使用该会话中的Cookie来获取下一个请求。
当我尝试这样做时,它给我401错误[身份验证问题]
def request_data(request_url, user, password, apiKey):
# These are the headers that will be passed with requests.
headers = {'Accept': 'application/json'}
# This headers sets where to start reporting from.
payload = {'apiKey': apiKey}
# Create the request session variable.
session = requests.Session()
# Build and make the request with url and your headers, auth, payload( reset value ).
# The first time is just to set the reset and get your auth/feed cookie.
resp = session.get(request_url, headers=headers, auth=(user, password), params=payload)
URL = 'https://admin.zscalerbeta.net/api/v1/urlCategories'
resp1 = session.get(URL)
return resp , session , resp1
def main():
# Hardcoded login details.
# This can be modifed to populate values from command line or config file.
# make call to request function and return json
resp , session , resp1 = request_data(auth_url, user, password, apiKey)
# This is just a sanity check print to verify you see your json data and cookies.
print(json.dumps(resp.json(), indent=4, sort_keys=True))
print(resp.cookies)
print(resp)
print(resp1)
# Checking URL Categories.
### SCRIPT ENTRY POINT ####
if __name__ == '__main__':
# Call to the main function.
main()
输出:
{
"obfuscateApiKey": true
}
<RequestsCookieJar[<Cookie JSESSIONID=9016354002CA0467A95DB87E913DEC84 for admin.zscalerbeta.net/>]>
<Response [200]>
<Response [401]>
我需要有关如何使其正常工作的帮助。