python - Paypal Rest API 不断返回格式错误的 json 错误

标签 python api rest paypal

我正在尝试使用 paypal Rest API 创建付款,但我不断收到此错误响应:

{"name":"MALFORMED_REQUEST","message":"The request JSON is not well formed.","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"262cbdc417df7"}

这里是我的代码:

payment_url = 'https://api.sandbox.paypal.com/v1/payments/payment'
headers = {"Content-Type": "application/json", "Authorization": "Bearer %s" % access_token}
data = {
        "intent": "sale",
        "redirect_urls": {
            "return_url": "http://localhost:8080/index.html",
            "cancel_url": "http://localhost:8080/index.html"
        },
        "payer": {
            "payment_method": "paypal"
        },
        "transactions": [
            {
                "amount": {
                    "total": "7.47",
                    "currency": "USD"
                },
                "details": {
                    "subtotal": "7.41",
                    "tax": "0.03",
                    "shipping": "0.03"
                },
                "description": "This is the payment transaction description.",
                "item_list": {
                    "items": [
                        {
                            "quantity": "1",
                            "name": "item",
                            "price": "7.41",
                            "currency": "USD",
                            "sku": "item"
                        }]
                }
            }
        ]
    }
    print headers
    print data
    r = requests.post(payment_url, headers=headers, data=data)
    print 'payment res', r.text

我只得到这样的回应:

{"name":"MALFORMED_REQUEST","message":"The request JSON is not well formed.","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"262cbdc417df7"}

我看到了很多关于这种错误的问题,但都没有解决方案。 :( json 格式的 post 数据显然是有效的。否则,请求 post 方法将引发异常。并且响应是从 paypal 服务器返回的,但我无法从它提供的链接中找到任何信息。我检查了 Rest API 文档,我想我提出的请求与样本完全一样。我错过了什么?

如有任何建议或解决方案,我们将不胜感激。

最佳答案

您的return_urlcancel_url 值需要用引号括起来。如消息所述,您的 JSON 格式不正确。

试试这个 - http://jsonlint.com/查看您的错误。

关于python - Paypal Rest API 不断返回格式错误的 json 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38952451/

相关文章:

.net - 电子签名/电子签名 API 建议

angularjs - Angular HttpInterceptor errorResponse 状态错误

java - 使用 Keycloak 保护的 REST API 作为 OAUTH2-Provider 的代理

c# - WCF 消耗多个可能的 REST 响应

python - Matplotlib Agg 渲染复杂度错误

python - 如何在Python中打开以下mat文件

c# - 在 AZURE 或本地部署 .net core 3.0

python - 如何在python 2环境conda中安装keras模块?

python - 将 pytorch 数据加载器加载到 GPU

python - Google Calendar API 日期时间格式 python