Django easypost buy returns 使用测试 api 键时语法格式错误但生产资金不足

Django easypost buy returns malformed syntax when using test api key but insufficient funds with production

使用 easypost python 库,我调用了购买函数,就像文档中所说的那样传递了价格,但它 returns 是一个错误。

您是否可以使用您的测试 api 密钥购买 easypost?我在文档中没有看到任何内容。它似乎适用于生产,但我还不能测试它,所以我想知道我是否可以用测试 api 键来测试它?

密码是:

import easypost

def get_shipment(shipment_id):
    return easypost.Shipment.retrieve(shipment_id)
......
 shipment = get_shipment(shipment_id)
 try:
    shipment.buy(rate=shipment.lowest_rate())
 except Exception as e:
     raise ValidationError({'detail': e.message})

我用测试密钥得到的错误信息

Traceback (most recent call last):
  File "/app/returns/serializers.py", line 237, in handle_shipment_purchase
    shipment.buy(rate=shipment.lowest_rate())
  File "/usr/local/lib/python3.6/dist-packages/easypost/__init__.py", line 725, in buy
    response, api_key = requestor.request('post', url, params)
  File "/usr/local/lib/python3.6/dist-packages/easypost/__init__.py", line 260, in request
    response = self.interpret_response(http_body, http_status)
  File "/usr/local/lib/python3.6/dist-packages/easypost/__init__.py", line 321, in interpret_response
    self.handle_api_error(http_status, http_body, response)
  File "/usr/local/lib/python3.6/dist-packages/easypost/__init__.py", line 383, in handle_api_error
    raise Error(error.get('message', ''), http_status, http_body)
easypost.Error: The request could not be understood by the server due to malformed syntax.

是的,您可以使用 TEST API 密钥购买货物。从您共享的代码中,我没有看到任何明显的问题,但您显然需要仔细检查您的 shipment_id 设置是否正确以及您的 API 密钥是否也正确设置。除此之外,请以 support@easypost.com 的身份写信给我们,我们实际上可以查看我们的系统日志以查看“格式错误”中可能出现的内容。

虽然我的货件编号和 API_KEY 是正确的,但我在 Python 上遇到了同样的问题。使用 EasyPost python 异常消息,它不会显示异常的根本原因。尝试使用 curl 或内部异常检查进行请求 e.json_body 并相应地引发 ValidationError。

 try:
    shipment.buy(rate=shipment.lowest_rate())
 except Exception as e:
    #  Put debugger here and Check exception e.json_body 
    e.json_body
    raise ValidationError({'detail': e.http_body})