Microsoft Graph 是 Microsoft 365 中通往数据和智能的网关。 它提供统一的可编程模型,可用于访问 Microsoft 365、Windows 10 和企业移动性 + 安全性中的海量数据。 利用 Microsoft Graph 中的大量数据针对与数百万名用户交互的组织和客户构建应用。
Microsoft Graph 公开了 REST API,包含了Azure上的所有资源操作。关于它目前能操作的资源可以参考官方说明:https://docs.microsoft.com/en-us/graph/api/overview?view=graph-rest-1.0
注:在中国区Microsoft Graph的终结点(Endpoint): https://microsoftgraph.chinacloudapi.cn/v1.0
问题描述
使用Microsoft Graph API 如何批量创建用户,用户属性中需要含有非AAD默认的Parameter(如Store_code, Store_name等)
解决办法
根据Graph API的文档说明,可以使用以下几个API组合完成以上需求:
Create User:Create a new user. The request body contains the user to create. At a minimum, you must specify the required properties for the user. You can optionally specify any other writable properties.
Create extensionProperty:Create a new extensionProperty definition. You can use this operation to add a custom property value to the targeted object type defined in the extensionProperty, using standard creation and update requests to the target object.
Batch Create:JSON batching allows you to optimize your application by combining multiple requests into a single JSON object.
测试示例
1) 为当前AAD添加扩展属性
POST https://microsoftgraph.chinacloudapi.cn/v1.0/applications/<objectID>/extensionProperties Body Content: {
"name": "store_code",
"dataType": "String",
"targetObjects": [
"User"
]
}
调用成功后,返回的Responce如下:
Response body:
{
"@odata.context": "https://microsoftgraph.chinacloudapi.cn/v1.0/$metadata#applications('31d886ad-b40b-4599-a708-3bf45948396b')/extensionProperties/$entity",
"id": "7d79ae82-6955-4e7f-b6a8-095c749a2cb8",
"deletedDateTime": null,
"appDisplayName": "test",
"dataType": "String",
"isSyncedFromOnPremises": false,
"name": "extension_c21xxxc9_store_code", "targetObjects": [ "User" ] }
2)调用创建User API
POST https://microsoftgraph.chinacloudapi.cn/v1.0/users Body Content
{
"accountEnabled": true,
"displayName": "test",
"mailNickname": "User",
"userPrincipalName": "test@MicrosoftInternal.partner.onmschina.cn",
"mobilePhone":"12345678909",
"surname":"MB",
"givenName":"1LTY2",
"jobTitle":"test",
"country":"china",
"postalCode":"178",
"department":"CN",
"officeLocation":"ccc","passwordProfile" : {
"forceChangePasswordNextSignIn": true,
"password": "xWwvJ]6NMw+bWH-d"
},
"extension_c21xxxc9_store_code":"11111",
"extension_c21xxxc9_store_name":"Test Store" }
3)批量创建用户
POST https://microsoftgraph.chinacloudapi.cn/v1.0/$batch
Body Content:
{
"requests": [
{
"id": "1",
"method": "POST",
"url": "/users",
"body": {
"accountEnabled": true,
"displayName": "test",
"mailNickname": "User",
"userPrincipalName": "test@MicrosoftInternal.partner.onmschina.cn",
"mobilePhone": "12345678909",
"surname": "MB",
"givenName": "1LTY2",
"jobTitle": "test",
"country": "china",
"postalCode": "178",
"department": "CN",
"officeLocation": "ccc",
"passwordProfile": {
"forceChangePasswordNextSignIn": true,
"password": "xWwvJ]6NMw+bWH-d"
},
"extension_c21xxxc9_store_code": "11111",
"extension_c21xxxc9_store_name": "Test Store"
},
"headers": {
"Content-Type": "application/json"
}
},
{
"id": "2",
"method": "POST",
"url": "/users",
"body": {
"accountEnabled": true,
"displayName": "test",
"mailNickname": "User 2",
"userPrincipalName": "test1@MicrosoftInternal.partner.onmschina.cn",
"mobilePhone": "12345678909",
"surname": "MB",
"givenName": "1LTY2",
"jobTitle": "test",
"country": "china",
"postalCode": "178",
"department": "CN",
"officeLocation": "ccc",
"passwordProfile": {
"forceChangePasswordNextSignIn": true,
"password": "xWwvJ]6NMw+bWH-d"
},
"extension_c21xxxc9_store_code": "22222",
"extension_c21xxxc9_store_name": "Test Store 2"
},
"headers": {
"Content-Type": "application/json"
}
}
]
}
参考资料
Microsoft Graph 概述:https://docs.microsoft.com/zh-cn/graph/overview?view=graph-rest-1.0
Create User:https://docs.microsoft.com/en-us/graph/api/user-post-users?view=graph-rest-1.0&tabs=http
Create extensionProperty:https://docs.microsoft.com/en-us/graph/api/application-post-extensionproperty?view=graph-rest-1.0&tabs=http
Combine multiple requests in one HTTP call using JSON batching: https://docs.microsoft.com/en-us/graph/json-batching?context=graph%2Fapi%2F1.0&view=graph-rest-1.0