Meraki.py returning 'bad request' when adding devices to a network

Solved
Crash_Override
Here to help

Meraki.py returning 'bad request' when adding devices to a network

I am new to using this library, but was excited to see it was available and how much easier it has been than translating curl to requests so far. However, I have run into an oddity that I have been so far unable to explain and am now throwing up a white flag to see if anyone has any ideas.

So, I am working on automating as much of my workflow as I possibly can for rolling out new retail locations where we use full Meraki stacks in an attempt to cut down on the time I have to invest in preparing and configuring every single site worth of devices. So far I already successfully am able to retrieve the OID from the provided API key, create a new dashboard network with our naming convention and copy settings from another retail store, collect the network ID of the new site, and update the SSID VLAN for the new site.... no problems!

Then comes a heap of 'bad requests' from trying to use adddevtonet() to claim devices to the new network so I can then set all that up. From the documentation from GitHub this method only requires the API key, the Network ID, and a Serial Number of a device. I have a function that reads through a CSV file of serial numbers for all the Meraki devices that are being deployed for this location as seen in the code snippet below:

 

 

# Claims devices from file to Meraki network
def addDevToNet(API_KEY, NID):
    with open("some_file.csv", newline="") as f:
        r = csv.reader(f)
        for s in r:
            meraki.adddevtonet(API_KEY, NID, s, suppressprint=False)

 

 

 
Now for whatever reason... it is not working. I thought maybe it was because these devices were not claimed into the Organization Inventory yet, so I went and claimed them all first to Org Inv, ran again... and low and behold same return of 'Bad Request'. Everything I have seen from people having similar issues has been a bad API key or not having API access enabled for the Org.... but clearly that is not the problem since all the other operations work just as they should using the same variables I am passing to this function. For s###s & giggles I revoked the API key and generated a new one just to rule out a bad API key... no change. 

I am sure this is something stupid simple that I am just overlooking or unaware of... but it has white hairs starting to grow in my beard... 

Any ideas?

1 Accepted Solution
Crash_Override
Here to help

<class 'str'>
XXXX-XXXX-XXXX
Device Operation Successful



<Response [200]>


I decided to mess with it a bit with slicing just to see what would work, and for whatever reason it is adding a single quote wrapper it seems. From my print statements I assumed that was just representing it was a string, but I guess it was actually part of the string. Now its working great.

View solution in original post

3 Replies 3
CBurkhead
Building a reputation

What is the format of the serial # in your CSV? I assume it has the dashes? The documentation for the https://api.meraki.com/api/v0/networks/{networkId}/devices/claim endpoint, which this function is calling expects them. Also, have you verified that your "s" value in your loop is just the string for the serial number and not a list or dictionary? The function converts what you pass for the serial number to a string, but if it is a list or dictionary, there will be extra data besides the serial # itself. Might want to run this with breakpoints in an IDE and see what your values of "s" look like in the debugger.

Crash_Override
Here to help

<class 'str'>
'XXXX-XXXX-XXXX'
Bad Request - See returned data for error details

<Response [200]>

 

Already have. Replaced the values for the SN for the snippet, but everything looks fine as it is a string and I am getting a 200 response. 

Crash_Override
Here to help

<class 'str'>
XXXX-XXXX-XXXX
Device Operation Successful



<Response [200]>


I decided to mess with it a bit with slicing just to see what would work, and for whatever reason it is adding a single quote wrapper it seems. From my print statements I assumed that was just representing it was a string, but I guess it was actually part of the string. Now its working great.

Get notified when there are additional replies to this discussion.