cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
482
Views
0
Helpful
2
Replies

Cisco DNAC - REST API Call to retrieve token.

Netmart
Level 3
Level 3

Hello,

For some reason, it appears that I am receiving the response for a token in request in html format indicated by the following error: 

 

  File "/usr/lib64/python3.9/json/decoder.py", line 355, in raw_decode

    raise JSONDecodeError("Expecting value", s, err.value) from None

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

 

Below you will find an excerpt of the code:

#!/usr/bin/python

import requests

import json

import urllib3

from requests.auth import HTTPBasicAuth

urllib3.disable_warnings()


my_dna_ctr="sandboxdnac2.cisco.com"

my_user="devnetuser"

my_password="Cisco123!"

tkn_headers={"Content-Type": "application/json"}

my_tkn_uri="https://"+my_dna_ctr+"/dna/system/api/vi/auth/token"

my_tkn_req=requests.post(my_tkn_uri, auth=HTTPBasicAuth(my_user, my_password),headers=tkn_headers,verify=False)

my_access_token=my_tkn_req.json()["Token"]

:

 

I appreciate any help.

Thanks.

 

2 Replies 2

mfr-6
Spotlight
Spotlight

hi @Netmart 

First - seems like your URL is wrong. It should be /dna/system/api/v1/auth/token <- there is "v1" instead of "vi".

I tested your script and unfortunately there is a problem with authentication endpoint on dnac. I got response:

 

{
    "error": "Basic Authorization is not allowed"
}

 


Seems like sandboxdnac2 has Basic auth disabled, because i tried to authenticate using GUI and Dev Toolkit - i got the same result.

BUT - you can use https://sandboxdnac.cisco.com/ without "2" and this will return you token with no issues so maybe this is the option?

The code with typo fixed:

 

#!/usr/bin/python
import requests
import json
import urllib3
from requests.auth import HTTPBasicAuth
urllib3.disable_warnings()


my_dna_ctr="sandboxdnac.cisco.com"
my_user="devnetuser"
my_password="Cisco123!"
tkn_headers={"Content-Type": "application/json"}

my_tkn_uri="https://"+my_dna_ctr+"/dna/system/api/v1/auth/token"
my_tkn_req=requests.post(my_tkn_uri, auth=HTTPBasicAuth(my_user, my_password),headers=tkn_headers,verify=False)

my_access_token=my_tkn_req.json()​["Token"]

 

 

Mateusz Frak NetDevOps | DevNet | Automation DevNet Expert #20240068
Please mark this post as helpful if it solves your issue, to make this visible for other users, thank you!