gpt4 book ai didi

python - 如何使用 Azure DevOps 的 python 客户端 API 将用户添加到 Azure DevOps?

转载 作者:行者123 更新时间:2023-12-01 07:43:08 27 4
gpt4 key购买 nike

我正在编写一个 python 脚本来将用户(来自 AAD 支持的提供商的现有用户)添加到 Azure DevOps。为此,我使用 Azure DevOps 的 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_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

它使用扩展 GraphUserCreationContext 的 GraphUserPrincipalNameCreationContext。

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

addAADUserContext = GraphUserCreationContext('anaya.john@domain.com')
print(addAADUserContext)
resp = graph_client.create_user(addAADUserContext)
print(resp)

但出现错误:

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

最佳答案

azure devops REST API 的 python 客户端中的

GraphUserCreationContext 类仅接受一个输入参数,即 StorageKey。因此,无论您提供给该函数作为输入参数,无论是 UPN 还是 ID,它都会被设置为存储键。

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

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

但是Graph客户端的create_user()函数需要在GraphUserCreationContext中设置originId或principalName之一作为输入参数。

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

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

  • GraphUserMailAddressCreationContext
  • GraphUserOriginIdCreationContext
  • GraphUserPrincipalNameCreationContext

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

您可以使用用户权利 - 为 azure devops 添加 REST API,而不是其 Graph 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 '

关于python - 如何使用 Azure DevOps 的 python 客户端 API 将用户添加到 Azure DevOps?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56591886/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com