Claiming devices with action batches, error 400

BrentG
Here to help

Claiming devices with action batches, error 400

Hello All,

 

I'm very new to the work of programming and I am stuck on some code I have written. The code is basically this: I have a CSV of device serial numbers and I want to claim them into our organization. I'm using the Action batches API to do this. When I run the code I have written thus far, I get a 400 response. I'm thinking that my payload is not correct, but it could be anything at this point.

 

Any help would be appreciated. Thanks.

 

Code:

 

# Import os to get api key
import requests
import json
import os
import csv

 

api_key = os.environ.get("API_KEY")
baseUrl = 'https://dashboard.meraki.com/api/v0/'

action_url = f"{baseUrl}organizations/XXXXXX/actionBatches"

 

headers = {
'Accept': '*/*',
'Content-Type': 'application/json',
'X-Cisco-Meraki-API-Key': api_key
}

 

# Open CSV file with serial numbers and print the headers
filename = 'meraki_ap.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)

 

for index, column_header in enumerate(header_row):
print(index, column_header)

 

# Get serial numbers of devices
serials = []
for row in reader:
serial = str(row[1])
serials.append(serial)
print(serials)

 

for serial in serials:
payload = """{
"confirmed": true,
"synchronous": false,
"actions": [
{
"resource": "/networks/L_XXXXXXXXXXXXX/devices/",
"operation": "claim",
"body": {
"serial": serial
}
}
]
}"""

 

claim = requests.post(action_url, headers=headers, data=payload)
print(claim)

3 Replies 3
chengineer
Meraki Alumni (Retired)
Meraki Alumni (Retired)

At a minimum, payload needs to be a dictionary, not a string as it is currently, so pass in data=json.dumps(payload) instead of data=payload

Solutions Architect @ Cisco Meraki | API & Developer Ecosystem
Edgar-VO
Building a reputation

Hi,

 

I see you are using python. Why not use the Meraki API.... 

This is designed for easy use in python

 

(pip3 -install meraki)

 

and import meraki in the header

 

I have a small subroutine which i use to add devices to the organisation....

 

Maybe it is of any use

 

def Claim_Org_Device(def_org,def_serial😞

    claimed = 0
    my_inventory = dashboard.organizations.getOrganizationInventory(org_id)

    for my_device in my_inventory:
        if my_device['serial'] == def_serial:
            claimed = 1

    if claimed == 0:

        my_serial = {}
        my_list = []
        my_list.append(def_serial)
        
        my_serial['serials'] = my_list

        try:    
            response = dashboard.organizations.claimOrganization(def_org,**my_serial)
        except:
            print ("Error adding serial to organisation")
            print (response)
            quit()

 

there is a small check if the device is already there, but you can remove it....

Also def_org = organisation id and def_serial is of course the serial number

 

Right now I'm just starting to learn Python so I'm trying to get a handle on that before moving on to the Meraki Dashboard API. Thanks for providing your script, I'll take a look at it and try it out.
Get notified when there are additional replies to this discussion.
Welcome to the Meraki Community!
To start contributing, simply sign in with your Cisco account. If you don't yet have a Cisco account, you can sign up.