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

2024-04-17

我正在编写一个 python 脚本,以使用 azure-devops-rest-api 的 python 客户端库在 azure devops 中创建用户。

我正在使用添加用户权利()的函数会员权利管理客户端.

链接到该客户端的代码:

https://github.com/microsoft/azure-devops-python-api/blob/dev/azure-devops/azure/devops/v5_0/member_entitlement_management/member_entitlement_management_client.py https://github.com/microsoft/azure-devops-python-api/blob/dev/azure-devops/azure/devops/v5_0/member_entitlement_management/member_entitlement_management_client.py

对应的REST API文档:

https://learn.microsoft.com/en-us/rest/api/azure/devops/memberentitlementmanagement/user%20entitlements/add?view=azure-devops-rest-4.1 https://learn.microsoft.com/en-us/rest/api/azure/devops/memberentitlementmanagement/user%20entitlements/add?view=azure-devops-rest-4.1

我写了代码:

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, "[email protected] /cdn-cgi/l/email-protection", 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': '[email protected] /cdn-cgi/l/email-protection', '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': '[email protected] /cdn-cgi/l/email-protection'}}

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 '

谁能帮我解决这个错误?


反序列化错误表明 python 无法反序列化对 type 对象的响应UserEntitlementsPostResponse。当响应不是预期的响应时,就会发生这种情况。

我编辑了add_user_entitlement()的函数MemberEntitlementManagementClientpython 客户端的类,添加一行来打印 POST 请求的响应。回应是:

{'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 组织中的“项目集合管理员”组后,脚本可以正常工作。

此步骤很重要,因为 Azure DevOps REST API 的 Microsoft 文档(https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/add-organization-users?view=azure-devops#pre先决条件 https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/add-organization-users?view=azure-devops#prerequisites)说:

要访问和管理用户,您必须拥有 Azure DevOps 项目 集合管理员或组织所有者权限。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐