cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1480
Views
0
Helpful
6
Replies

Cisco DNAC - Token Generation via Postman

Netmart
Level 1
Level 1

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

 

 


.


.

1 Accepted Solution

Accepted Solutions

Netmart
Level 1
Level 1

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"]))

View solution in original post

6 Replies 6

Marcel Zehnder
Spotlight
Spotlight

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

 

Netmart
Level 1
Level 1

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:

 

Error 404

Page not found. Contact your administrator for details.

 

Netmart
Level 1
Level 1

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'

 

 

 
 

 

 

 

 

Try adding `requests.packages.urllib3.disable_warnings()` 

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

Netmart
Level 1
Level 1

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: 

#!/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()

payload = {}

headers = {
    'X-Auth-Token':
    'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2MWJhMzlhYTY1NzY0NDBlN2NhY2M5OWMiLCJhdXRoU291cmNlIjoiZXh0ZXJuYWwiLCJ0ZW5hbnROYW1lIjoiVE5UMCIsInJvbGVzIjpbIjYyYmNiNmIwYjBlODZhNzI0ZDQ1YWI4MyJdLCJ0ZW5hbnRJZCI6IjVmZTM5ZjgyNTM2M2ExMDBiZjY3OTVjMCIsImV4cCI6MTY1NzkwNjg4OSwiaWF0IjoxNjU3OTAzMjg5LCJqdGkiOiI3Y2ZlNjg3NC05NGViLTQxZWEtYTZiOC0xMjlhN2VlMDdlY2EiLCJ1c2VybmFtZSI6IndvbGZtIn0.d4_xaGuaQZZPzcyJiogIaXwOwa1Bzat-1yhq7JQg5frUhXCJJvcSc4QvwpPYYi6XBKalzHjOli-ZPlp5h_W-Pp-XDX8EH-BkL0rcrdAZmwIq9ynw2k5XFWoQIWjprUQ0miGj6BlQsTigmmPok4XY0UmwSA__5DydoXuWj1f1gAN0gwae5OV9ZhLCkD06Itv6hHgOIq2pI7dbrIqu1a0gaMp-V1i9jdp9AIDGUSSBOgGQWr2AOS34uDb3nal56EM4XuCtHKNAKXlfvihPB8EAoXJLu4GTyuLABN28gtrg6uf1Is6XToCLu7OXZNkdwrYCfnR2Ykut3W3SPGvXi0oaqg'
}
response = requests.request("GET", url, verify=Falseheaders=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"]))
 

Netmart
Level 1
Level 1

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"]))