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

时间:2016-08-15 09:21:28

标签: 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方法会引发异常。而响应是从paypal服务器返回的,但是我无法从它给出的链接中找到任何信息。我已经检查了Rest API文档,我认为我完全按照样本提出了请求。我错过了什么?

任何善意的建议或解决方案都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

您的 return_url cancel_url 值需要在它们周围加上引号。正如消息所说,您的JSON格式不正确。

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