英文:
How to POST customFieldValues with SynchroTeam API using Python request?
问题
Q: 如何使用Python POST请求更新SynchroTeam中的customFieldValues?
链接:https://api.synchroteam.com/#custom-field-values
我花了整整一天的时间来处理这个问题,得到了一个500错误响应,没有任何线索告诉我我做错了什么。在得到了一些来自SynchroTeam支持的帮助后,我终于弄清楚了。
英文:
Q: How should one update the customFieldValues in SynchroTeam using a Python POST request?
https://api.synchroteam.com/#custom-field-values
I spent the past day working on this getting a Error 500 response, and no clue to tell me what I was doing wrong. With a bit of help from the SynchroTeam support, I was able to figure this out.
答案1
得分: 1
这是我用来完成这个任务的代码:
import base64
import requests
# 如果你不知道你自定义字段的ID...
def get_customer_customfields():
url = f"https://ws.synchroteam.com/Api/v3/customfield/list"
payload = {"type": "customer"}
headers = get_headers()
try:
response = requests.get(url=url, params=payload, headers=headers)
return response.json()
except Exception as e:
print(f"Error {e}")
# 获取你的POST请求所需的头部信息...
def get_headers() -> dict:
domain = "YOUR SYNCHROTEAM DOMAIN"
api_key = "YOUR SYNCHROTEAM API_KEY"
auth_key = str(base64.b64encode(bytes(f"{domain}:{api_key}", 'utf-8')))
auth_key = auth_key[2:][:-1] # 移除第一个2个字符和最后一个字符后再使用
return {
'accept': 'text/json',
'authorization': f"Basic {auth_key}",
'cache-control': 'no-cache',
'content-type': 'application/json'
}
# 如何更新自定义字段的值...
def update_customer():
url = "https://ws.synchroteam.com/Api/v3/customer/send"
# 在这个示例中,我正在更新客户 3530636 的 `Our Customer ID` 字段值
updates = {
'id': 3530636,
'customFieldValues': [
{
'id': 221313, # 通过调用 get_customer_customfields() 来找到这个ID
'label': 'Our Customer ID',
'value': 'CID-00016813'
}
]
}
headers = get_headers()
try:
response = requests.post(url=url, json=updates, headers=headers)
if (response.status_code==200):
return response.json() # 一切正常
else:
raise RuntimeError(response.text)
except Exception as e:
print(f"Error {e}")
请注意,代码中的 "YOUR SYNCHROTEAM DOMAIN" 和 "YOUR SYNCHROTEAM API_KEY" 部分需要根据你的具体情况替换为实际的域名和API密钥。
英文:
Here's the code I used to accomplish this task:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
import base64
import requests
# in case you don't know the ID of your custom field(s)...
def get_customer_customfields():
url = f"https://ws.synchroteam.com/Api/v3/customfield/list"
payload = {"type": "customer"}
headers = get_headers()
try:
response = requests.get(url=url, params=payload, headers=headers)
return response.json()
except Exception as e:
print(f"Error {e}")
# how to get the headers you'll need for your POST request...
def get_headers() -> dict:
domain = "YOUR SYNCHROTEAM DOMAIN"
api_key = "YOUR SYNCHROTEAM API_KEY"
auth_key = str(base64.b64encode(bytes(f"{domain}:{api_key}", 'utf-8')))
auth_key = auth_key[2:][:-1] # remove 1st 2 chars and last char before using it
return {
'accept': 'text/json',
'authorization': f"Basic {auth_key}",
'cache-control': 'no-cache',
'content-type': 'application/json'
}
# how to update the customFieldValue(s)...
def update_customer():
url = "https://ws.synchroteam.com/Api/v3/customer/send"
# in this example, I'm updating customer 3530636 with a
# new value for `Our Customer ID`
updates = {
'id': 3530636,
'customFieldValues': [
{
'id': 221313, # find this id by calling get_customer_customfields()
'label': 'Our Customer ID',
'value': 'CID-00016813'
}
]
}
headers = get_headers()
try:
response = requests.post(url=url, json=updates, headers=headers)
if (response.status_code==200):
return response.json() # all is well
else:
raise RuntimeError(response.text)
except Exception as e:
print(f"Error {e}")
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论