如何使用Python请求在SynchroTeam API中POST customFieldValues?

huangapple go评论51阅读模式
英文:

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&#39;t know the ID of your custom field(s)...
def get_customer_customfields():
url = f&quot;https://ws.synchroteam.com/Api/v3/customfield/list&quot;
payload = {&quot;type&quot;: &quot;customer&quot;}
headers = get_headers()
try:
response = requests.get(url=url, params=payload, headers=headers)
return response.json()
except Exception as e:
print(f&quot;Error {e}&quot;)
# how to get the headers you&#39;ll need for your POST request...
def get_headers() -&gt; dict:
domain = &quot;YOUR SYNCHROTEAM DOMAIN&quot;
api_key = &quot;YOUR SYNCHROTEAM API_KEY&quot;
auth_key = str(base64.b64encode(bytes(f&quot;{domain}:{api_key}&quot;, &#39;utf-8&#39;)))
auth_key = auth_key[2:][:-1]  # remove 1st 2 chars and last char before using it
return {
&#39;accept&#39;: &#39;text/json&#39;,
&#39;authorization&#39;: f&quot;Basic {auth_key}&quot;,
&#39;cache-control&#39;: &#39;no-cache&#39;,
&#39;content-type&#39;: &#39;application/json&#39;
}
# how to update the customFieldValue(s)...
def update_customer():
url = &quot;https://ws.synchroteam.com/Api/v3/customer/send&quot;
# in this example, I&#39;m updating customer 3530636 with a 
# new value for `Our Customer ID`
updates = {
&#39;id&#39;: 3530636, 
&#39;customFieldValues&#39;: [
{
&#39;id&#39;: 221313,  # find this id by calling get_customer_customfields()
&#39;label&#39;: &#39;Our Customer ID&#39;, 
&#39;value&#39;: &#39;CID-00016813&#39;
}
]
}
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&quot;Error {e}&quot;)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月11日 05:05:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定