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

简介: 根据Graph API文档,在单个请求中将多个成员添加到组时,Python代码示例中的`[email protected]`被错误写为`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代码示例中,把 [email protected] 错误的写成了 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={
            "[email protected]": [
                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

 


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

相关文章
|
2月前
|
API 网络安全 网络架构
【Azure APIM】解答REST API实现"禁用自签名证书的证书链验证"中的backends参数值从那里取值的问题?
本文介绍APIM服务调用后端API时因自签名证书导致500错误的解决方案。通过REST API禁用证书链验证,关键在于获取正确的backendId(即APIM中配置的Backend名称),并调用PATCH接口设置validateCertificateChain为false,从而解决SSL/TLS信任问题。
179 6
|
2月前
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南
|
2月前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。
|
2月前
|
测试技术 Python
Python装饰器:为你的代码施展“魔法”
Python装饰器:为你的代码施展“魔法”
268 100
|
2月前
|
开发者 Python
Python列表推导式:一行代码的艺术与力量
Python列表推导式:一行代码的艺术与力量
436 95
|
3月前
|
Python
Python的简洁之道:5个让代码更优雅的技巧
Python的简洁之道:5个让代码更优雅的技巧
270 104
|
3月前
|
开发者 Python
Python神技:用列表推导式让你的代码更优雅
Python神技:用列表推导式让你的代码更优雅
480 99
|
2月前
|
缓存 Python
Python装饰器:为你的代码施展“魔法
Python装饰器:为你的代码施展“魔法
162 88
|
2月前
|
Cloud Native 算法 API
Python API接口实战指南:从入门到精通
🌟蒋星熠Jaxonic,技术宇宙的星际旅人。深耕API开发,以Python为舟,探索RESTful、GraphQL等接口奥秘。擅长requests、aiohttp实战,专注性能优化与架构设计,用代码连接万物,谱写极客诗篇。
Python API接口实战指南:从入门到精通
|
2月前
|
缓存 数据可视化 定位技术
快递鸟快递API技术指南:获取物流轨迹信息与轨迹地图的解决方案
在当今电商竞争激烈的环境中,物流体验已成为提升用户满意度的关键因素。研究表明,超过 75% 的消费者会因物流信息不透明而放弃下单。
588 1

推荐镜像

更多