如何使用用户的python客户端API将用户添加到Azure DevOps? [英] How to add a user to Azure DevOps using it's python client API?

查看:106
本文介绍了如何使用用户的python客户端API将用户添加到Azure DevOps?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个python脚本,以将用户(AAD支持的提供程序中的现有用户)添加到Azure DevOps.我正在为此目的使用Azure DevOps的python客户端库. 身份验证之后,我可以通过以下方式从azure devops中获取用户:

I am writing a python script to add a user(an existing user from the AAD backed provider) to Azure DevOps. I am using python client library of Azure DevOps for this purpose. After authentication, I am able to fetch the users from azure devops as:

# 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连接添加用户?

How to add a user using this GraphClient connection?

这里有一个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

There is a create_user function ( use as graph_client.create_user() ) here to do this: https://github.com/microsoft/azure-devops-python-api/blob/dev/azure-devops/azure/devops/v5_0/graph/graph_client.py

它表示请求应包含GraphUserCreationContext作为输入参数.

It says that the request should include a GraphUserCreationContext as an input parameter.

但是我如何为AAD用户获取GraphUserCreationContext?我只有有关AAD用户的UPN的信息作为输入.

But how can I get that GraphUserCreationContext for an AAD user? I only have information about the AAD user's UPN as input.

注意:

我发现.NET示例可以在此处执行此操作: https://github.com/microsoft/azure-devops-dotnet-samples/blob/master/ClientLibrary/Samples/Graph/UsersSample.cs

I found .NET sample to do this here : https://github.com/microsoft/azure-devops-dotnet-samples/blob/master/ClientLibrary/Samples/Graph/UsersSample.cs

它使用GraphUserPrincipalNameCreationContext来扩展GraphUserCreationContext.

It uses GraphUserPrincipalNameCreationContext which extends GraphUserCreationContext.

但是我在python客户端库中找不到这样的类.我使用了这样的代码:

But i couldn't find such a class in python client library. I used the code like this:

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.

python客户端中的

推荐答案

GraphUserCreationContext类,用于天蓝色devops REST API仅接受一个输入参数,即StorageKey.因此,无论您提供给该功能的输入参数是UPN还是ID,都将其设置为存储键.

GraphUserCreationContext class from the python client for azure devops REST API accepts only one input parameter which is StorageKey. Hence, whatever you provide as an input parameter to that function, be it a UPN or ID, it is set as a storage key.

如果打印addAADUserContext对象,则会得到:

If you print the addAADUserContext object, you will get:

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

但是Graph客户端的create_user()函数需要恰好在它作为输入参数的GraphUserCreationContext中设置的originId或principalName之一.

But the create_user() function of Graph client needs exactly one of originId or principalName set in the GraphUserCreationContext it takes as input parameter.

作为azure devops REST API的Microsoft文档(

As the microsoft documentaion for the azure devops REST API (https://docs.microsoft.com/en-us/rest/api/azure/devops/graph/users/create?view=azure-devops-rest-4.1 ) :

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

The body of the request must be a derived type of GraphUserCreationContext:

  • GraphUserMailAddressCreationContext
  • GraphUserOriginIdCreationContext
  • GraphUserPrincipalNameCreationContext

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

We shouldn't use the GraphUserCreationContext object directly. But the classes like GraphUserPrincipalNameCreationContext aren't currently available in the python client API. They are working on it. You can track the issue here in GitHub repo: https://github.com/microsoft/azure-devops-python-api/issues/176

您可以使用User Entitlements(用户权利)-为azure devops添加REST API,而不是它的Graph API.为此,您可以使用以下python客户端:

You can use User Entitlements - Add REST API for azure devops instead of it's Graph API. You can use the following python client for this purpose:

https ://github.com/microsoft/azure-devops-python-api/tree/dev/azure-devops/azure/devops/v5_0/member_entitlement_management

您可以参考以下问题中给出的示例,以了解如何使用所提到的python客户端:

You can refer to the sample given in the following question to know about how to use the mentioned python client :

无法反序列化为对象:类型,KeyError:'key:int;值:str'

这篇关于如何使用用户的python客户端API将用户添加到Azure DevOps?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆