python - 如何使用它的 python 客户端 API 将用户添加到 Azure DevOps?
问题描述
我正在编写一个 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
它使用 GraphUserPrincipalNameCreationContext 扩展 GraphUserCreationContext。
但是我在 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.
解决方案
GraphUserCreationContext
来自用于 azure devops REST API 的 python 客户端的类仅接受一个输入参数,即StorageKey
. 因此,无论您提供什么作为该函数的输入参数,无论是 UPN 还是 ID,它都会被设置为存储密钥。
如果打印addAADUserContext
对象,您将获得:
{'additional_properties': {}, 'storage_key': 'anaya.john@domain.com'}
但是 Graph 客户端的功能恰好需要它作为输入参数create_user()
设置的 originId 或 principalName 之一。GraphUserCreationContext
作为 azure devops REST API 的微软文档(https://docs.microsoft.com/en-us/rest/api/azure/devops/graph/users/create?view=azure-devops-rest-4.1):
请求的主体必须是 GraphUserCreationContext 的派生类型:
- GraphUserMailAddressCreationContext
- GraphUserOriginIdCreationContext
- GraphUserPrincipalNameCreationContext
我们不应该GraphUserCreationContext
直接使用该对象。但是像 GraphUserPrincipalNameCreationContext 这样的类目前在 python 客户端 API 中不可用。他们正在努力。您可以在 GitHub 存储库中跟踪问题:https ://github.com/microsoft/azure-devops-python-api/issues/176
您可以使用 User Entitlements - Add REST API for azure devops 而不是它的 Graph API。为此,您可以使用以下 python 客户端:
您可以参考以下问题中给出的示例以了解如何使用上述 python 客户端:
推荐阅读
- c# - 如何禁用特定行并防止在 Xamarin 数据网格中单击?
- reactjs - React Grid 上的弹出图像
- python - Python 使用 pytest_mock 在一个函数中模拟多个查询
- powershell - 如何使用 PowerShell 在 GUI 表单中处理标签大小和位置?
- c# - .Net HttpClient - 403:请启用cookies
- .htaccess - .htaccess 301 重定向除主页外的所有页面
- c# - 用于多种访问模式的 dynamoDb 范围键设计
- javascript - 开玩笑 - 断言异步函数抛出测试失败
- java - Swing 组件如何考虑我的默认语言环境?
- php - 如何使用 Symfony 原则指定我需要应用迁移的架构?