简体   繁体   中英

Microsoft Graph API Nest JSON Request is failed

I am developing App leveraging Microsoft Graph using Python 3.6.

I am getting very strange behavior when requesting Graph API which uses request data as nested JSON.

This is a successful request:

url = f "https://graph.microsoft.com/v1.0/users/{user_id}"
headers = {
  'Authorization': f 'Bearer {office365_access_token}',
  'Content-Type': 'application/json'
}
data = {
  "city": "Tokyo"
}
req = urllib.request.Request(url, json.dumps(data).encode("utf-8"), headers = headers, method = 'PATCH')
urllib.request.urlopen(req)

The next snipped fails with an HTTP Error 400 error. The documentation states that the skills property is a String Collection, so I used an Array of String values:

url = f "https://graph.microsoft.com/v1.0/users/{user_principal_name}"
headers = {
  'Authorization': f 'Bearer {office365_access_token}',
  'Content-Type': 'application/json'
}
data = {
  "skills": ["swift", "python"]
}
req = urllib.request.Request(url, json.dumps(data).encode("utf-8"), headers = headers, method = 'PATCH')
urllib.request.urlopen(req)

The only difference is whether the value is a string or not. I can dump the data dictionary to a JSON string, so I don't think the code is wrong but I do not know why this error occurres.

It appears to be a bug related with Microsoft Graph itself, specifically with User update operation . For example the following query:

PATCH https://graph.microsoft.com/v1.0/me
Content-type: application/json
{
  "skills": [
    "Fortran",
    "Cobol"
  ],
  "city": "Helsinki"
}

indeed fails and returns the following error:

{
    "error": {
        "code": "BadRequest",
        "message": "The request is currently not supported on the targeted entity set"
    }
}

At the same time updating another User properties, for example User.otherMails property which has the same Collection(Edm.String) type as User.skills :

PATCH https://graph.microsoft.com/v1.0/me
Content-type: application/json


{
  "otherMails": [
    "office365admin@gmail.com",
    "office365admin@yahoo.com"
  ],
  "city": "Helsinki"
}

completes successfully.

Workaround

It appears it fails when skills property of User resource is getting updated along with another properties. But if only a skills property is getting updated

PATCH https://graph.microsoft.com/v1.0/me
Content-type: application/json

{
  "skills": [
    "Fortran",
    "Cobol",
    "C"
  ]
}

no error occurs and the operation successfully completes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM