python - 如何使用 Python 从 Square 中的 create customer 方法中检索客户 ID
问题描述
我正在创建一个正方形的客户并得到如下结果。我需要的是获取客户的 ID。
我的代码:
from square.client import Client
client = Client(
access_token=settings.SQUARE_ACCESS_TOKEN,
environment=settings.SQUARE_ENVIRONMENT,
)
api_customers = client.customers
request_body = {'idempotency_key': idempotency_key, 'given_name': name, 'company_name': company,'phone_number':phone}
result = api_customers.create_customer(request_body)
这是输出:
<ApiResponse [{"customer":
{"id": "F8M9KDHWPMYGK2108RMQVQ6FHC",
"created_at": "2020-10-22T09:14:50.159Z",
"updated_at": "2020-10-22T09:14:50Z",
"given_name": "mkv5",
"phone_number": "900000066666",
"company_name": "codesvera",
"preferences": {"email_unsubscribed": false},
"creation_source": "THIRD_PARTY"}
}
]>
解决方案
你在使用这个库吗? https://github.com/square/square-python-sdk/blob/master/square/http/api_response.py
如果是,则结果是一个数组和 APiResponse 对象。
所以首先你应该这样做:result = result.body
然后获取ID: result['customer']['id']
Ps:您在 github 文档中有示例: https ://github.com/square/square-python-sdk
# Initialize the customer count
total_customers = 0
# Initialize the cursor with an empty string since we are
# calling list_customers for the first time
cursor = ""
# Count the total number of customers using the list_customers method
while True:
# Call list_customers method to get all customers in this Square account
result = api_customers.list_customers(cursor)
if result.is_success():
# If any customers are returned, the body property
# is a list with the name customers.
# If there are no customers, APIResponse returns
# an empty dictionary.
if result.body:
customers = result.body['customers']
total_customers += len(customers)
# Get the cursor if it exists in the result else set it to None
cursor = result.body.get('cursor', None)
print(f"cursor: {cursor}")
else:
print("No customers.")
break
# Call the error method to see if the call failed
elif result.is_error():
print(f"Errors: {result.errors}")
break
# If there is no cursor, we are at the end of the list.
if cursor == None:
break
print(f"Total customers: {total_customers}")
推荐阅读
- html - 当我链接到 jquery 文件时,我还必须输入 type="text/javascript"
- javascript - TypeError: Cannot read properties of undefined (reading 'map') 似乎找不到函数的问题
- rabbitmq - 带有 RabbitMQ 插件的 ThingsBoard
- javascript - React 组件 HTML 内容来自 GET 请求,我需要以某种方式从 HTML 元素中删除一个元素
- javascript - Laravel 混合,使用许多输出文件进行更快的 sass 编译
- python - Azure 中的问题在小型 csv 上使用 Python 的 read_csv 部署了 Databricks。Py4j:获取新通信通道时出错
- amazon-web-services - 在 AWS Code Pipeline 中集成漏洞扫描
- security - SAML 断言中的 SAML 2.0 摘要值计算
- matlab - 如何绘制 ODE 系统解的分量的导数?
- google-chrome - 如何禁用 Lingocloud - 自动翻译页面内容的 Web 翻译扩展