首页 > 解决方案 > 如何从 cognito api 响应中获取 phone_no

问题描述

呼唤认知admingetuser

response = client.admin_get_user( UserPoolId='string', Username='string' )

作为回应,我回来response

response {'Username': 'xyz@gmail.com', 'UserAttributes': [{'Name': 'sub', 'Value': 'cb0328a8-38fd-4799-84ef-4f7f2733016e'}, {'Name': 'email_verified', 'Value': 'true'}, {'Name': 'phone_number_verified', 'Value': 'false'}, {'Name': 'phone_number', 'Value': '+445115115551'}, {'Name': 'custom:account_id', 'Value': 'a54a936f-d846-44ec-8f6f-c9e127991bda'}, {'Name': 'email', 'Value': 'xyz@gmail.com'}], 'UserCreateDate': datetime.datetime(2018, 12, 10, 13, 46, 47, 533000, tzinfo=tzlocal()), 'UserLastModifiedDate': datetime.datetime(2018, 12, 14, 16, 24, 20, 707000, tzinfo=tzlocal()), 'Enabled': True, 'UserStatus': 'CONFIRMED', 'ResponseMetadata': {'RequestId': '5c649fd3-1005-11e9-b484-433d044b60ba', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Fri, 04 Jan 2019 09:44:42 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '500', 'connection': 'keep-alive', 'x-amzn-requestid': '5c649fd3-1005-11e9-b484-433d044b60ba'}, 'RetryAttempts': 0}}"

我想从 python 中的这个响应中提取 phone_no 我该怎么做?有什么建议么?

标签: python

解决方案


根据cognito 文档,响应是 JSON 格式。

因此,您可以简单地将响应解析为 json 使用json.loads(response). 这样你可能会得到字典列表。此列表元素之一将是字典包含phone_number。该字典中键下的值Value是电话号码。


推荐阅读