Ryan
Ryan

Reputation: 333

MS Graph/assignLicense: primitive 'null' value was expected

From python, I'm trying to assign a license to a user in Office365 using MS Graph API.

request_url = "https://graph.microsoft.com/v1.0/users/[email protected]/assignLicense"
headers = { 
 'Authorization' : 'Bearer ' + token,
 'Content-Type' : 'application/json',
}

response = requests.post(url = request_url, headers = headers, json={'addLicenses': 'testGUID'})

However, I am getting the following error message:

{
  "error": {
    "code": "Request_BadRequest",
    "innerError": {
      "date": "2018-08-27T15:56:45",
      "request-id": "9ddde6c8-5fe1-4425-ba84-bc49fa35e2b8"
    },
    "message": "When trying to read a null collection parameter value in JSON Light, a node of type 'PrimitiveValue' with the value 'test' was read from the JSON reader; however, a primitive 'null' value was expected."
  }
}

How can I call assignLicense from python?

Upvotes: 1

Views: 842

Answers (1)

Marc LaFleur
Marc LaFleur

Reputation: 33114

You're assigning a GUID to addLicenses which is incorrect. From the documentation, addLicenses is defined as:

A collection of assignedLicense objects that specify the licenses to add.

In other words, it's an array of assignedLicense objects. In order to assign a license to a user, you need to send the following JSON payload:

{
  "addLicenses": [
    {
      "disabledPlans":[ ],
      "skuId": "guid"
    }
  ],
  "removeLicenses":[ ]
}

I believe the Python for this would look something like this:

request_url = "https://graph.microsoft.com/v1.0/users/[email protected]/assignLicense"
headers = { 
 'Authorization' : 'Bearer ' + token,
 'Content-Type' : 'application/json',
}
data = [
    "addLicenses": [
    {
      "disabledPlans":[ ],
      "skuId": "GUID"
    }
  ],
  "removeLicenses":[ ]
]
requests.post(url, json=data, headers=headers)

Upvotes: 2

Related Questions