[英]Microsoft Graph API Nest JSON Request is failed
I am developing App leveraging Microsoft Graph using Python 3.6.我正在使用 Python 3.6 开发利用 Microsoft Graph 的应用程序。
I am getting very strange behavior when requesting Graph API which uses request data as nested JSON.在请求使用请求数据作为嵌套 JSON 的 Graph API 时,我得到了非常奇怪的行为。
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.下一个剪切失败,并显示
HTTP Error 400
错误。 The documentation states that the skills
property is a String Collection, so I used an Array of String values: 文档指出,
skills
属性是一个字符串集合,所以我使用了一个字符串值数组:
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.
我可以将数据字典转储为 JSON 字符串,所以我认为代码没有错,但我不知道为什么会出现此错误。
It appears to be a bug related with Microsoft Graph itself, specifically with User
update operation .这似乎是一个与 Microsoft Graph 本身相关的错误,特别是与
User
更新操作有关。 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
:同时更新的另一用户的特性,对于具有同样的例子User.otherMails属性
Collection(Edm.String)
类型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.当
User
资源的skills
属性与其他属性一起更新时,它似乎失败了。 But if only a skills
property is getting updated但如果只有
skills
属性得到更新
PATCH https://graph.microsoft.com/v1.0/me
Content-type: application/json
{
"skills": [
"Fortran",
"Cobol",
"C"
]
}
no error occurs and the operation successfully completes.没有错误发生并且操作成功完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.