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

时间:2019-06-14 05:28:45

标签: 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=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.

2 个答案:

答案 0 :(得分:1)

用于zure 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(https://docs.microsoft.com/en-us/rest/api/azure/devops/graph/users/create?view=azure-devops-rest-4.1)的Microsoft文档:

  

请求的正文必须是GraphUserCreationContext的派生类型:

     
      
  • GraphUserMailAddressCreationContext
  •   
  • GraphUserOriginIdCreationContext
  •   
  • GraphUserPrincipalNameCreationContext
  •   

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

您可以使用User Entitlements(用户权利)-为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 '

答案 1 :(得分:0)

我使用他们的模型创建了这个类

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