如何使用 python 客户端 API 将用户添加到 Azure DevOps?
How to add a user to Azure DevOps using it's python client API?
我正在编写 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 客户端库中找不到这样的 class。我使用了这样的代码:
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
class 来自 python 客户端的 azure devops REST API 只接受一个输入参数 StorageKey
。因此,无论您作为该函数的输入参数提供什么,无论是 UPN 还是 ID,它都会被设置为存储密钥。
如果您打印 addAADUserContext
对象,您将得到:
{'additional_properties': {}, 'storage_key': 'anaya.john@domain.com'}
但是Graph客户端的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 ) :
The body of the request must be a derived type of GraphUserCreationContext:
- GraphUserMailAddressCreationContext
- GraphUserOriginIdCreationContext
- GraphUserPrincipalNameCreationContext
我们不应该直接使用 GraphUserCreationContext
对象。但是像 GraphUserPrincipalNameCreationContext 这样的 classes 目前在 python 客户端 API 中不可用。他们正在努力。您可以在 GitHub 存储库中跟踪此问题:https://github.com/microsoft/azure-devops-python-api/issues/176
您可以使用用户权利 - 为 azure devops 添加 REST API 而不是 Graph API。为此,您可以使用以下 python 客户端:
您可以参考以下问题中给出的示例,了解如何使用提到的 python 客户端:
我使用他们的模型
创建了这个class
class GraphUserAADCreationContext(Model):
"""
:param principal_name: The principal name from AAD like 'user@mydomain.com'
:type principal_name: str
:param storage_key: Optional: If provided, we will use this identifier for the storage key of the created user
:type storage_key: str
"""
_attribute_map = {
'storage_key': {'key': 'storageKey', 'type': 'str'},
'principal_name': {'key': 'principalName', 'type': 'str'}
}
def __init__(self, storage_key=None, principal_name=None):
super(GraphUserAADCreationContext, self).__init__()
self.storage_key = storage_key
self.principal_name = principal_name
您可以使用这个 class 的实例作为输入参数而不是 GraphUserCreationContext
我正在编写 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 客户端库中找不到这样的 class。我使用了这样的代码:
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
class 来自 python 客户端的 azure devops REST API 只接受一个输入参数 StorageKey
。因此,无论您作为该函数的输入参数提供什么,无论是 UPN 还是 ID,它都会被设置为存储密钥。
如果您打印 addAADUserContext
对象,您将得到:
{'additional_properties': {}, 'storage_key': 'anaya.john@domain.com'}
但是Graph客户端的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 ) :
The body of the request must be a derived type of GraphUserCreationContext:
- GraphUserMailAddressCreationContext
- GraphUserOriginIdCreationContext
- GraphUserPrincipalNameCreationContext
我们不应该直接使用 GraphUserCreationContext
对象。但是像 GraphUserPrincipalNameCreationContext 这样的 classes 目前在 python 客户端 API 中不可用。他们正在努力。您可以在 GitHub 存储库中跟踪此问题:https://github.com/microsoft/azure-devops-python-api/issues/176
您可以使用用户权利 - 为 azure devops 添加 REST API 而不是 Graph API。为此,您可以使用以下 python 客户端:
您可以参考以下问题中给出的示例,了解如何使用提到的 python 客户端:
我使用他们的模型
创建了这个classclass GraphUserAADCreationContext(Model):
"""
:param principal_name: The principal name from AAD like 'user@mydomain.com'
:type principal_name: str
:param storage_key: Optional: If provided, we will use this identifier for the storage key of the created user
:type storage_key: str
"""
_attribute_map = {
'storage_key': {'key': 'storageKey', 'type': 'str'},
'principal_name': {'key': 'principalName', 'type': 'str'}
}
def __init__(self, storage_key=None, principal_name=None):
super(GraphUserAADCreationContext, self).__init__()
self.storage_key = storage_key
self.principal_name = principal_name
您可以使用这个 class 的实例作为输入参数而不是 GraphUserCreationContext