【Azure Developer】Python代码调用Graph API将外部用户添加到组,结果无效,也无错误信息

简介: 根据Graph API文档,在单个请求中将多个成员添加到组时,Python代码示例中的`members@odata.bind`被错误写为`members@odata_bind`,导致用户未成功添加。

问题描述

根据Graph API的实例文档,在单个请求中将多个成员添加入组。 代码执行后,无错误消息,但是,用户也没有添加成功。

在单个请求中向组添加多个成员

文档地址 :https://learn.microsoft.com/zh-cn/graph/api/group-post-members?view=graph-rest-1.0&tabs=python

 

问题解答

在文档中,对比HTTP / C# / JS / Powershell 代码中的additional  data结构,发现一个不同点:

Python代码示例中,把 members@odata.bind 错误的写成了 members@odata_bind

 

当把 “ _ ” 修改为正确的 “ . " 后,添加操作执行成功!

 

(Python) 完成实现代码如下:


from azure.identity import ClientSecretCredential
# from msgraph.generated.models.invitation import Invitation
from kiota_authentication_azure.azure_identity_authentication_provider import AzureIdentityAuthenticationProvider
import asyncio
from msgraph.generated.models.unified_role_assignment import UnifiedRoleAssignment
from msgraph import GraphServiceClient, GraphRequestAdapter
from msgraph.generated.models.group import Group
from msgraph.generated.models.reference_create import ReferenceCreate
from msgraph.generated.models.unified_role_assignment import UnifiedRoleAssignment
# Values from app registration
tenant_id = 'xx-x-x-x-xxxx'
client_id = 'xx-x-x-x-xxxx'
client_secret = '***'
# azure.identity.aio
credential = ClientSecretCredential(
    tenant_id=tenant_id,
    client_id=client_id,
    client_secret=client_secret)
scopes = ['https://microsoftgraph.chinacloudapi.cn/.default']
graph_client = GraphServiceClient(credential, scopes)  # type: ignore
auth_provider = AzureIdentityAuthenticationProvider(credential, scopes=scopes)
request_adapter = GraphRequestAdapter(auth_provider)
request_adapter.base_url = https://microsoftgraph.chinacloudapi.cn/v1.0/
graph_client = GraphServiceClient(request_adapter=request_adapter)
async def add_user_to_group():
    request_body = Group(
        additional_data={
            "members@odata.bind": [
                https://microsoftgraph.chinacloudapi.cn/v1.0/directoryObjects/{id},
                https://microsoftgraph.chinacloudapi.cn/v1.0/directoryObjects/{id},
            ],
        }
    )
    result = await graph_client.groups.by_group_id('xx-x-x-x-xxxx').patch(request_body)
    if result:
        print(result)
asyncio.run(add_user_to_group())

 

 

参考资料

在单个请求中向组添加多个成员:https://learn.microsoft.com/zh-cn/graph/api/group-post-members?view=graph-rest-1.0&tabs=python

 


当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

相关文章
|
7月前
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南
|
7月前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。
|
7月前
|
测试技术 Python
Python装饰器:为你的代码施展“魔法”
Python装饰器:为你的代码施展“魔法”
372 100
|
7月前
|
开发者 Python
Python列表推导式:一行代码的艺术与力量
Python列表推导式:一行代码的艺术与力量
559 95
|
7月前
|
缓存 Python
Python装饰器:为你的代码施展“魔法
Python装饰器:为你的代码施展“魔法
440 88
|
开发框架 jenkins 持续交付
跨平台API对接(Python)的使用
跨平台API对接(Python)的使用
|
开发框架 jenkins 持续交付
跨平台API对接(Python)的使用
![](https://ceshiren.com/uploads/default/original/3X/3/a/3a86a19fb6dbb3f346088c7323fa31227d08207b.png) ## Python-Jenkins Python-Jenkins 通过 HTTP 方式运行 Jenkins job 。 Python-Jenkins 官网:https://pypi.py
|
开发框架 jenkins 持续交付
跨平台API对接(Python)的使用
跨平台API对接(Python)的使用
|
8月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1326 102
|
8月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
473 104

推荐镜像

更多