07-12-2022 11:11 AM
Hello,
I am trying to generate a token ro apply a an API call to Cisco DNAC.
According to documentation, this requires a token generated by creating a POST method:
https://<IP of Cisco DNAC/dna/system/api/v1/auth/token
What authorization type is recommended to use - I tried basic authentication and no authentication and received "404 Not found'
Please advise
.
.
Solved! Go to Solution.
07-15-2022 11:16 AM
Ok, I think I found the culprit: the URL string - since this is an API related to DNAC northbound, I have put in the proper string - pointing to intent.
GET:
version < 1.2.6: https://<IP ADDRESS>/api/intent/v1/network-device
version >= 1.2.6: https://<IP ADDRESS>/dna/intent/api/ v1/network-device
In my example:
DNAC version 2.2.3.5: POST https://<IP ADDRESS>/dna/intent/api/ v1/network-device
And here is the whole script. Keep in mind that this script does require to retrieve the token from postman.
#!/usr/bin/python3
import requests
import json
from prettytable import PrettyTable
print ("start");
# API URL to get a List of Network devices
requests.packages.urllib3.disable_warnings()
url = "https://<IP Address>/dna/intent/api/v1/network-device"
payload = {}
headers = {
'X-Auth-Token':
'<Paste Token retrieved via Postman.'
}
response = requests.request("GET", url, verify=False, headers=headers, data = payload)
# Take the response and turn it into Python Objects
raw_output = json.loads(response.text)
# raw_output is a Dictionary. Take the value of response and assign to devices
devices = raw_output["response"]
#Iterate through the list to print out hostnames of devices
for device in devices:
print("Hostname: {}".format(device["hostname"]))
07-13-2022 06:44 AM
BasicAuth is just fine - what's your DNAC version, there was a change for the auth-uri:
version < 1.2.6: https://<IP ADDRESS>/api/system/v1/auth/token
version >= 1.2.6: https://<IP ADDRESS>/dna/system/api/v1/auth/token
07-13-2022 08:31 PM
Hi Marcel,
We do run version 2.2.3.5.
And I tried the ne you suggested:
https://<IP ADDRESS>/dna/system/api/v1/auth/token
Still:
Page not found. Contact your administrator for details.
07-13-2022 10:08 PM
After Disabling SSL warnings in Postman, I was able to retrieve the token.
It tried to run the following script:
#!/usr/bin/python3
import requests
import json
from prettytable import PrettyTable
print ("start");
# API URL to get a List of Network devices
url = "https://dnac/dna/system/api/v1/network-device"
payload = {}
headers = {
'X-Auth-Token':
'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2MWJhMzlhYTY1NzY0NDBlN2NhY2M5OWMiLCJhdXRoU291cmNlIjoiZXh0ZXJuYWwiLCJ0ZW5hbnROYW1lIjoiVE5UMCIsInJvbGVzIjpbIjYyYmNiNmIwYjBlODZhNzI0ZDQ1YWI4MyJdLCJ0ZW5hbnRJZCI6IjVmZTM5ZjgyNTM2M2ExMDBiZjY3OTVjMCIsImV4cCI6MTY1Nzc3ODI0NywiaWF0IjoxNjU3Nzc0NjQ3LCJqdGkiOiJjOWUxM2U5MS1hNDk3LTQ4NWQtYTgzNi0yNTI3NjA2MTM1ODciLCJ1c2VybmFtZSI6IndvbGZtIn0.JNpNkFvHNqWqThV19bYPMHvbHyzvbXXE0gt8-o-XinBw1MwbDxH0M0ICqFAbH6E63BXeCVHoC7txhQPBi3YovyF4emGdgEIS0L_OVn7cbyLgYWydVeyxEqDrXMXjmnrhENb1ZRYdYfxY5qB_JSDkVpj0VNOXNca4dB84TylyONMDVdUYS0Jg1bBNOJJeDbSd1x6BrGbxS989RVeApfqAI2xaratMgBXXWxFHV5Daic3TYpohcyjEAuuNCRzDFC-EvoW-k8ws1kLqAlIk_IpQJn2IriNTVKOPe6QXUpXFEdaAbKjB5aMxM1jNOH7CAiM2JxNV1epKbncyVA66GlPTDg'
}
response = requests.request("GET", url, verify=False, headers=headers, data = payload)
# Take the response and turn it into Python Objects
raw_output = json.loads(response.text)
# raw_output is a Dictionary. Take the value of response and assign to devices
devices = raw_output["response"]
#Iterate through the list to print out hostnames of devices
for device in devices:
print("Hostname: {}".format(device["hostname"]))
And the following error was received:
C:\Python38\lib\site-packages\urllib3\connectionpool.py:846: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
warnings.warn((
Traceback (most recent call last):
File "./DeviceList_v1.py", line 25, in <module>
devices = raw_output["responses"]
KeyError: 'response'
07-14-2022 02:51 AM
Try adding `requests.packages.urllib3.disable_warnings()`
07-15-2022 09:54 AM
Thank you Stuart. I remember your post about: https://blogs.cisco.com/developer/python-scripting-apis
When I add "`requests.packages.urllib3.disable_warnings()` ", I still receive the error:
\Scripts>C:/Python38/python.exe i:/C/Scripts/DeviceList_v1.py
start
Traceback (most recent call last):
File "i:/NCS/Cisco/Cisco_DNAC/Scripts/DeviceList_v1.py", line 25, in <module>
devices = raw_output["response"]
KeyError: 'response'
And this is the whole script:
07-15-2022 11:16 AM
Ok, I think I found the culprit: the URL string - since this is an API related to DNAC northbound, I have put in the proper string - pointing to intent.
GET:
version < 1.2.6: https://<IP ADDRESS>/api/intent/v1/network-device
version >= 1.2.6: https://<IP ADDRESS>/dna/intent/api/ v1/network-device
In my example:
DNAC version 2.2.3.5: POST https://<IP ADDRESS>/dna/intent/api/ v1/network-device
And here is the whole script. Keep in mind that this script does require to retrieve the token from postman.
#!/usr/bin/python3
import requests
import json
from prettytable import PrettyTable
print ("start");
# API URL to get a List of Network devices
requests.packages.urllib3.disable_warnings()
url = "https://<IP Address>/dna/intent/api/v1/network-device"
payload = {}
headers = {
'X-Auth-Token':
'<Paste Token retrieved via Postman.'
}
response = requests.request("GET", url, verify=False, headers=headers, data = payload)
# Take the response and turn it into Python Objects
raw_output = json.loads(response.text)
# raw_output is a Dictionary. Take the value of response and assign to devices
devices = raw_output["response"]
#Iterate through the list to print out hostnames of devices
for device in devices:
print("Hostname: {}".format(device["hostname"]))
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide