通过 SendGrid API 更新 SendGrid 联系人自定义字段为什么这不起作用?

问题描述

我正在尝试更新我的 SendGrid 联系人,但不知道为什么我尝试更新我的联系人的自定义字段不起作用。我的保留字段(名字、姓氏、电子邮件)更新,但我的自定义字段没有。任何想法为什么?

此处的文档:https://sendgrid.api-docs.io/v3.0/contacts/add-or-update-a-contact

    try:
        headers = {
            'authorization': f"Bearer {settings.SENDGRID_API_KEY}",}
        data = {
            "list_ids": [
                # "Users" list
                "7c2...d20"
            ],"contacts": [{
                "email": user.email,"first_name": user.first_name,"last_name": user.last_name,"custom_fields": {
                    "educator_role": user.educator_role,}
            }]
        }

        response = requests.put("https://api.sendgrid.com/v3/marketing/contacts",headers=headers,data=json.dumps(data))
        if(response.status_code != 202):
            capture_message(f"Could not add user with email {user.email} to Sendgrid.",level="error")
    except:
        capture_message(f"Adding/updating SendGrid contact Failed for {user.email}.",level="error")```

解决方法

与保留字段不同,更新自定义字段需要您在调用中传递自定义字段 id 而不是字段 name。因此,不要使用 educator_role,而是使用 id,它将是随机的,例如 e1_T

您可以通过 /marketing/field_definitions 端点获取 ID。