python - Zoho 调用 API 出现问题 {"code":1038 ,"message":"JSON is not well formed»}

标签 python api zoho

我正在努力使用发票 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 格式不正确»}

您能帮助我并告诉我应该如何对数据进行编码吗?

最佳答案

正如 Klaus 在您的问题中所指出的,这里的问题可能是您设置的“ContentType” header 。我在使用 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())

关于python - Zoho 调用 API 出现问题 {"code":1038 ,"message":"JSON is not well formed»},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52574490/

相关文章:

api - 使用 PHP 通过 API 将记录插入到 Zoho CRM

python - 在一些字符后替换字符串

python - 使用PhoneGap + Google App Engine上传和保存图像

python - 为什么在使用 next() 时出现 StopIteration 错误?

mysql - 将 Angular 应用程序连接到服务器

django - SMTPDataError at/accounts/signup/(553, b'不允许作为 webmaster@localhost 进行中继')

python - nltk pos 标签咒语

python - 努力通过 Python 理解 Guardian API

c# - 访问自签名证书详细信息

sql - SQL 语法中的 LAG 和 LEAD 替代方案