无法反序列化为object:type,KeyError:'key:int;值:str' [英] Unable to deserialize to object: type, KeyError: ' key: int; value: str '

查看:103
本文介绍了无法反序列化为object:type,KeyError:'key:int;值:str'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在使用 MemberEntitlementManagementClient add_user_entitlement()功能.

链接到该客户端的代码:

https://docs.microsoft.com/zh-cn/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, "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 '

有人可以帮助我解决此错误吗?

解决方案

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

我通过添加一行来打印POST请求的响应来编辑python客户端的MemberEntitlementManagementClient类的add_user_entitlement()函数.答复是:

{'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://github.com/microsoft/azure-devops-python-api/blob/dev/azure-devops/azure/devops/v5_0/member_entitlement_management/member_entitlement_management_client.py

Corresponding REST API documentation:

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

I wrote the code:

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)  

But I got the output with an error 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 '

Can anyone help me to solve this error?

解决方案

The deserialization error says that python cannot deserialize the response got to an object of type UserEntitlementsPostResponse. This happens when the response is not the expected one.

I edited the add_user_entitlement() function of MemberEntitlementManagementClient class of the python client by adding a line to print the response of the POST request. The response was:

{'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}

Obviously, this was not the expected result. And this cannot be converted to a UserEntitlementsPostResponse type object. Hence, the error occurred.

As the error suggests, after adding the user (who runs the script) to a group 'project collection administrators' in our azure devops organization, the script worked correctly.

This step was important as the microsoft documentation for azure devops REST API ( https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/add-organization-users?view=azure-devops#prerequisites ) says:

To access and manage users, you must have Azure DevOps project collection administrator or organization owner permissions.

这篇关于无法反序列化为object:type,KeyError:'key:int;值:str'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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