如何使用azuredevops的python客户端API向其添加用户?

2024-05-16 22:33:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个python脚本来向azuredevops添加一个用户(来自AAD支持的提供商的现有用户)。为此,我使用了azuredevops的python客户端库。 经过身份验证后,我可以从azure devops获取用户:

# Create a connection to the org
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)

# Get a client (the "graph" client provides access to list,get and create user)
graph_client = connection.clients_v5_0.get_graph_client()
resp = graph_client.list_users()

# Access the properties of object as object.property
users = resp.graph_users

# Show details about each user in the console
for user in users:
    pprint.pprint(user.__dict__)
    print("\n")

如何使用此GraphClient连接添加用户?在

这里有一个create\u user函数(用作graph_client.create_user())来完成此操作:https://github.com/microsoft/azure-devops-python-api/blob/dev/azure-devops/azure/devops/v5_0/graph/graph_client.py

它说请求应该包含一个GraphUserCreationContext作为输入参数。在

但是我怎样才能为AAD用户获取GraphUserCreationContext呢?我只有AAD用户的UPN信息作为输入。在

注意:

我在这里找到了.NET示例:https://github.com/microsoft/azure-devops-dotnet-samples/blob/master/ClientLibrary/Samples/Graph/UsersSample.cs

它使用了GraphUserPrincipalNameCreationContext,它扩展了GraphUserCreationContext。在

但我在python客户端库中找不到这样的类。我用的代码是这样的:

^{pr2}$

但有个错误:

azure.devops.exceptions.AzureDevOpsServiceError: VS860015: Must have exactly one of originId or principalName set.

Tags: theto用户client客户端createconnectionazure
1条回答
网友
1楼 · 发布于 2024-05-16 22:33:18

来自python client for azure devops REST API的GraphUserCreationContext类只接受一个输入参数StorageKey。因此,无论您提供什么作为该函数的输入参数,不管它是UPN还是ID,它都被设置为一个存储键。在

如果打印addAADUserContext对象,将得到:

{'additional_properties': {}, 'storage_key': 'anaya.john@domain.com'}

但是Graph client的create_user()函数正好需要GraphUserCreationContext中设置的originId或principalName中的一个作为输入参数。在

作为azure devops REST API(https://docs.microsoft.com/en-us/rest/api/azure/devops/graph/users/create?view=azure-devops-rest-4.1)的microsoft文档:

The body of the request must be a derived type of GraphUserCreationContext:

  • GraphUserMailAddressCreationContext
  • GraphUserOriginIdCreationContext
  • GraphUserPrincipalNameCreationContext

我们不应该直接使用GraphUserCreationContext对象。但是像GraphUserPrincipalNameCreationContext这样的类目前在python客户机API中不可用。他们正在努力。您可以在GitHub repo中跟踪该问题:https://github.com/microsoft/azure-devops-python-api/issues/176

您可以使用用户授权-添加用于azure devops的RESTAPI,而不是它的图形API。为此,可以使用以下python客户端:

https://github.com/microsoft/azure-devops-python-api/tree/dev/azure-devops/azure/devops/v5_0/member_entitlement_management

您可以参考以下问题中给出的示例来了解如何使用上述python客户机:

Unable to deserialize to object: type, KeyError: ' key: int; value: str '

相关问题 更多 >