【发布时间】:2021-11-27 04:14:39
【问题描述】:
我正在尝试在没有任何 SDK 或包的情况下在 Django 中实现 PayPal。
https://developer.paypal.com/docs/business/checkout/server-side-api-calls/create-order/
想将此 cURL 重写为 Python
curl -v -X POST https://api-m.sandbox.paypal.com/v2/checkout/orders \
-H "Content-Type: application/json" \
-H "Authorization: Bearer Access-Token" \
-d '{
"intent": "CAPTURE",
"purchase_units": [
{
"amount": {
"currency_code": "USD",
"value": "100.00"
}
}
]
}'
我当前的代码:
t = gettoken()
d = {"intent": "CAPTURE","purchase_units": [{"amount": {"currency_code": "USD","value": "100.00"}}]}
h = {"Content-Type: application/json", "Authorization: Bearer "+t}
r = requests.post('https://api-m.sandbox.paypal.com/v2/checkout/orders', headers=h, data=d).json()
我的错误:
Internal Server Error: /createOrder
.....
AttributeError: 'set' object has no attribute 'items'
不记名令牌没问题。
有什么想法吗?我错过了什么?
【问题讨论】:
-
正确的标题是
{"Content-Type": "application/json", "Authorization": "Bearer "+t}。您当前拥有的实际上是set而不是dict因此错误 -
谢谢。现在我得到 '{'name': 'INVALID_REQUEST', 'message': '请求格式不正确、语法不正确或违反架构。', 'debug_id': '252e2f9a9587d', 'details': [{ 'location': 'body', 'issue': 'MALFORMED_REQUEST_JSON', 'description': '请求 JSON 格式不正确'}], 'links': [{'href': 'developer.paypal.com/docs/api/orders/v2/…', ' rel': 'information_link', 'encType': 'application/json'}]} 那么我的 d 部分有什么错误吗?
-
看起来不错。它适用于 curl 吗?