如何获取AAD用户的GraphUserPrincipalNameCreationContext?

时间:2019-06-14 12:22:41

标签: python api graph azure-devops client

我需要使用Azure DevOps REST API的python客户端库在azure devops中创建一个新用户。

我编写了以下代码:

from azure.devops.connection import Connection
from azure.devops.v5_0.graph.models import GraphUserCreationContext
from msrest.authentication import BasicAuthentication

credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)

graph_client = connection.clients_v5_0.get_graph_client()

addAADUserContext = GraphUserCreationContext("anaya.john@mydomain.com")
print(addAADUserContext)

resp = graph_client.create_user(addAADUserContext)
print(resp) 

我得到输出:

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

调用create_user方法时发生错误:

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

实际上我应该将 GraphUserPrincipalNameCreationContext 传递给create_user函数。

我找到了一个.NET示例,该示例在名为 AddRemoveAADUserByUPN()的函数中进行了此操作: https://github.com/microsoft/azure-devops-dotnet-samples/blob/master/ClientLibrary/Samples/Graph/UsersSample.cs

GraphUserPrincipalNameCreationContext是此示例中的接口。但是python不支持接口。

那么如何在python中实现呢?

1 个答案:

答案 0 :(得分:1)

Python客户端API中目前不提供某些类,例如GraphUserPrincipalNameCreationContext。他们正在努力。您可以在GitHub存储库中跟踪此问题: https://github.com/microsoft/azure-devops-python-api/issues/176

您可以将用户权利-添加 REST API用于Azure开发,而不是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 '