AnjK
AnjK

Reputation: 3583

How to get the GraphUserPrincipalNameCreationContext for an AAD user?

I need to create a new user in azure devops using the python client library for Azure DevOps REST API.

I wrote the following code:

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("[email protected]")
print(addAADUserContext)

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

I get the output:

{'additional_properties': {}, 'storage_key': '[email protected]'}

And an error occurs while calling the create_user method:

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

Actually what i should pass a GraphUserPrincipalNameCreationContext to the create_user function.

I found a .NET sample which does this in a function named AddRemoveAADUserByUPN() : https://github.com/microsoft/azure-devops-dotnet-samples/blob/master/ClientLibrary/Samples/Graph/UsersSample.cs

GraphUserPrincipalNameCreationContext is an interface in this sample. But python doesn't support interfaces.

So how can implement this in python?

Upvotes: 1

Views: 195

Answers (1)

AnjK
AnjK

Reputation: 3583

Some of 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

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

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

Unable to deserialize to object: type, KeyError: ' key: int; value: str '

Upvotes: 1

Related Questions