python - Zoho 调用 API 的问题 {"code":1038,"message":"JSON 格式不正确»}
问题描述
我正在为发票 API 苦苦挣扎。所以我使用以下 Python 脚本来创建发票,但它似乎是错误的:
import urllib.parse
import requests
import json
data = { "customer_id": 982000000567001, "contact_persons": [ "982000000870911", "982000000870915" ], "invoice_number": "INV-00003", "reference_number": " ", "place_of_supply": "TN", "gst_treatment": "business_gst", "gst_no": "22AAAAA0000A1Z5", "template_id": 982000000000143, "date": "2013-11-17", "payment_terms": 15, "payment_terms_label": "Net 15", "due_date": "2013-12-03", "discount": 0, "is_discount_before_tax": "true", "discount_type": "item_level", "is_inclusive_tax": "false", "exchange_rate": 1, "recurring_invoice_id": " ", "invoiced_estimate_id": " ", "salesperson_name": " ", "custom_fields": [ { "label": "Record Number", "value": 23 } ], "project_id": " ", "line_items": [ { "item_id": 982000000030049, "project_id": " ", "time_entry_ids": [ {} ], "expense_id": " ", "name": "Hard Drive", "product_type": "goods", "hsn_or_sac": 80540, "item_order": 1, "rate": 120, "quantity": 1, "unit": " ", "discount": 0, "tax_id": 982000000557028, "tax_exemption_id": 11149000000061054, "tax_name": "VAT", "tax_type": "tax", "tax_percentage": 12.5, "item_total": 120 } ], "payment_options": { "payment_gateways": [ { "configured": "true", "additional_field1": "standard", "gateway_name": "paypal" } ] }, "allow_partial_payments": "true", "custom_body": " ", "custom_subject": " ", "notes": "Looking forward for your business.", "terms": "Terms & Conditions apply", "shipping_charge": 0, "adjustment": 0, "adjustment_description": " ", "reason": " ", "tax_authority_id": 11149000000061052, "tax_exemption_id": 11149000000061054 }
headers = {"content-type": "application/x-www-form-urlencoded;charset=UTF-8","Authorization":"Zoho-authtoken <MY_TOKEN>","X-com-zoho-invoice-organizationid": "<MY_ORGANIZATION_ID>"}
url = "https://invoice.zoho.com/api/v3/invoices"
r = requests.post(url, data=json.dumps(data), headers=headers)
print(r.json())
我得到的是: {"code":1038,"message":"JSON 格式不正确»}
你能帮我告诉我应该如何编码我的数据吗?
解决方案
正如克劳斯在您的问题上指出的那样,这里的问题很可能是您设置的“ContentType”标头。我在 Zoho 的订阅 API 上遇到了类似的问题,但通过以下方式解决了它:
import requests
import json
from pprint import pprint
# This Script Will Need To Be Updated to new OAUTH SOON
ZOHO_AUTHTOKEN = 'PUTYOURAUTHTOKENHERE'
DUMMY_ORGANIZATION_ID = 'OrganizationIDFROMZOHO'
BASE_URL = "https://subscriptions.zoho.com/api/v1"
headers = {"Authorization": "Zoho-authtoken " + ZOHO_AUTHTOKEN}
subscription_id = '1234'
dict_data = {} #Put required data to send here
json_string = json.dumps(dict_data)
headers['ContentType'] = 'application/json'
updated_subscription_request = requests.put(BASE_URL+"/subscriptions/"+subscription_id, headers=headers, params={
"organization_id": DUMMY_ORGANIZATION_ID}, data=json_string)
pprint(updated_subscription_request.json())
推荐阅读
- types - 什么是归纳定义的数据类型?
- java - For Loop 得分保龄球
- python - 蟒蛇 | tkinter:tkinter.END 做什么?
- authentication - 回送facebook登录
- javascript - 在错误位置的 HTML 画布上绘图 - Javascript
- swift - 如何绕过将“自我”传递给 showWindow 方法?
- c# - 计时器范围和正确处置
- ruby-on-rails - 使用用户传递的参数作为在服务器端执行的 shell 命令的一部分是否危险
- dm-script - CleanImage() 的相反命令是什么
- sql - 取消嵌套嵌套的 SQL 查询