我正在编写一个python脚本,使用azure-devops-rest-api的python客户端库在azure devops中创建用户。
我正在使用 MemberEntitlementManagementClient 的 add_user_entitlement()功能。
链接到该客户端的代码:
相应的REST API文档:
我写了代码:
from azure.devops.connection import Connection
from azure.devops.v5_0.member_entitlement_management.models import *
import pprint
personal_access_token = <my token>
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
member_ent_mngmnt_client = connection.clients_v5_0.get_member_entitlement_management_client()
# List member entitlements
resp = member_ent_mngmnt_client.get_user_entitlements()
print(resp)
# ------ Add user entitlement -----------------
access_level = AccessLevel("express", None, None, None, None, None, None)
print(access_level)
graph_user = GraphUser(None, None, None, None, None, None, None, "user", None, None, "anaya.john@dynactionize.onmicrosoft.com", None, None, None)
print(graph_user)
user_entitlement = UserEntitlement(None, None, None, None, None, None, graph_user)
print(user_entitlement)
# This is to check what is sent as the request body of REST API POST request
content = member_ent_mngmnt_client._serialize.body(user_entitlement, 'UserEntitlement')
print("\n Content : \n")
print(content)
# Add user entitlement
resp = member_ent_mngmnt_client.add_user_entitlement(user_entitlement)
print("\n Result: \n")
print(resp)
但是我得到的输出是错误 msrest.exceptions.DeserializationError::
{'additional_properties': {}, 'account_license_type': 'express', 'assignment_source': None, 'license_display_name': None, 'licensing_source': None, 'msdn_license_type': None, 'status': None, 'status_message': None}
{'additional_properties': {}, '_links': None, 'descriptor': None, 'display_name': None, 'url': None, 'legacy_descriptor': None, 'origin': None, 'origin_id': None, 'subject_kind': 'user', 'domain': None, 'mail_address': None, 'principal_name': 'anaya.john@mydomain.com', 'is_deleted_in_origin': None, 'metadata_update_date': None, 'meta_type': None}
{'additional_properties': {}, 'access_level': None, 'extensions': None, 'group_assignments': None, 'id': None, 'last_accessed_date': None, 'project_entitlements': None, 'user': <azure.devops.v5_0.member_entitlement_management.models.GraphUser object at 0x000002147F444FD0>}
Content :
{'user': {'subjectKind': 'user', 'principalName': 'anaya.john@dynactionize.onmicrosoft.com'}}
Traceback (most recent call last):
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1294, in _deserialize
value = self.deserialize_data(raw_value, attr_desc['type'])
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1447, in deserialize_data
return self.deserialize_type[iter_type](data, data_type[1:-1])
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1478, in deserialize_iter
return [self.deserialize_data(a, iter_type) for a in attr]
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1478, in <listcomp>
return [self.deserialize_data(a, iter_type) for a in attr]
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1447, in deserialize_data
return self.deserialize_type[iter_type](data, data_type[1:-1])
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1494, in deserialize_dict
return {k: self.deserialize_data(v, dict_type) for k, v in attr.items()}
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1494, in <dictcomp>
return {k: self.deserialize_data(v, dict_type) for k, v in attr.items()}
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1449, in deserialize_data
obj_type = self.dependencies[data_type]
KeyError: ' key: int; value: str '
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/Anjana/Desktop/scripts_O365/az_devops_clientAPI_PAT.py", line 152, in <module>
resp = member_ent_mngmnt_client.add_user_entitlement(user_entitlement)
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\azure\devops\v5_0\member_entitlement_management\member_entitlement_management_client.py", line 184, in add_user_entitlement
return self._deserialize('UserEntitlementsPostResponse', response)
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1228, in __call__
return self._deserialize(target_obj, data)
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1294, in _deserialize
value = self.deserialize_data(raw_value, attr_desc['type'])
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1460, in deserialize_data
return self._deserialize(obj_type, data)
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1298, in _deserialize
raise_with_traceback(DeserializationError, msg, err)
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\exceptions.py", line 51, in raise_with_traceback
raise error.with_traceback(exc_traceback)
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1294, in _deserialize
value = self.deserialize_data(raw_value, attr_desc['type'])
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1447, in deserialize_data
return self.deserialize_type[iter_type](data, data_type[1:-1])
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1478, in deserialize_iter
return [self.deserialize_data(a, iter_type) for a in attr]
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1478, in <listcomp>
return [self.deserialize_data(a, iter_type) for a in attr]
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1447, in deserialize_data
return self.deserialize_type[iter_type](data, data_type[1:-1])
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1494, in deserialize_dict
return {k: self.deserialize_data(v, dict_type) for k, v in attr.items()}
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1494, in <dictcomp>
return {k: self.deserialize_data(v, dict_type) for k, v in attr.items()}
File "C:\Users\Anjana\AppData\Local\Programs\Python\Python37\lib\site-packages\msrest\serialization.py", line 1449, in deserialize_data
obj_type = self.dependencies[data_type]
msrest.exceptions.DeserializationError: Unable to deserialize to object: type, KeyError: ' key: int; value: str '
有人可以帮助我解决此错误吗?
答案 0 :(得分:0)
反序列化错误表明python无法反序列化对类型为UserEntitlementsPostResponse
的对象的响应。当响应不是预期的响应时,就会发生这种情况。
我通过添加一行来打印POST请求的响应来编辑python客户端的add_user_entitlement()
类的MemberEntitlementManagementClient
函数。答复是:
{'operationResult': {'isSuccess': False, 'errors': [{'key': 5032, 'value': 'Access Denied: This user needs the following permission(s) on the resource Users to perform this action: Add Users'}], 'userId': '261d25ad091b', 'result': None}, 'isSuccess': False, 'userEntitlement': None}
显然,这不是预期的结果。并且不能将其转换为UserEntitlementsPostResponse
类型的对象。因此,发生了错误。
正如错误所暗示的那样,在将用户(运行脚本的用户)添加到我们的azure devops组织中的“项目集合管理员”组后,脚本可以正常工作。
这一步骤非常重要,因为Microsoft Azure Azure REST API(https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/add-organization-users?view=azure-devops#prerequisites)文档说:
要访问和管理用户,您必须具有Azure DevOps项目 集合管理员或组织所有者权限。