如何使用Azure DevOps的python客户端API向其添加用户?
我正在编写一个python脚本,将一个用户(一个来自AAD支持的提供商的现有用户)添加到Azure DevOps。为此,我正在使用Azure DevOps的python客户端库。 身份验证后,我可以通过以下方式从azure devops获取用户:如何使用Azure DevOps的python客户端API向其添加用户?,python,api,azure-devops,client,azure-devops-rest-api,Python,Api,Azure Devops,Client,Azure Devops Rest 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
# 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_客户端。此处的create_user可执行以下操作:
它表示请求应该包含一个GraphUserCreationContext作为输入参数
但是我如何才能为AAD用户获取GraphUserCreationContext呢?我只有关于AAD用户UPN的信息作为输入
注:
我发现.NET示例可以在此处执行此操作:
它使用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.
azure devops REST API python客户端的GraphUserCreationContext类只接受一个输入参数,即StorageKey。因此,无论您提供什么作为该函数的输入参数,无论是UPN还是ID,它都被设置为存储密钥
如果打印addAADUserContext对象,您将获得:
{'additional_properties': {}, 'storage_key': 'anaya.john@domain.com'}
但是Graph client的create_user函数只需要GraphUserCreationContext中设置的originId或principalName中的一个作为输入参数
作为azure devops REST API的microsoft文档:
请求正文必须是GraphUserCreationContext的派生类型:
GraphUserMailAddressCreationContext
GraphuseOriginidCreationContext
GraphUserPrincipalNameCreationContext
我们不应该直接使用GraphUserCreationContext对象。但是像GraphUserPrincipalNameCreationContext这样的类目前在python客户端API中不可用。他们正在做这件事。您可以在GitHub repo中跟踪此问题:
您可以使用用户权限-为azure devops添加REST API,而不是它的Graph API。为此,您可以使用以下python客户端:
您可以参考以下问题中给出的示例,了解如何使用提到的python客户端:
我使用他们的模型创建了这个类
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
您可以将此类的实例作为输入参数,而不是GraphUserCreationContext