-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_token.py
50 lines (32 loc) · 1.04 KB
/
auth_token.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
"""
Author: Nick Russo
Purpose: Demonstrate Python "requests" to get an access token
from Cisco DNA Center using the REST API.
"""
import requests
def get_token():
"""
Gets an access token from Cisco DNA Center. Returns the token
string if successful; raises HTTPError otherwise.
"""
# Declare useful local variables to simplify request process
api_path = "https://sandboxdnac2.cisco.com"
auth = ("dnacdev", "D3v93T@wK!")
headers = {"Content-Type": "application/json", "Authorization": "Basic YWRtaW46R3JhcGV2aW5lMQ=="}
# Issue HTTP POST request to the proper URL to request a token
auth_resp = requests.post(
f"{api_path}/api/system/v1/auth/token", auth=auth, headers=headers
)
# If successful, print token. Else, raise HTTPError with details
auth_resp.raise_for_status()
token = auth_resp.json()["Token"]
return token
def main():
"""
Execution begins here.
"""
token = get_token()
print(token)
if __name__ == "__main__":
main()