【问题标题】:Updating SendGrid contact custom fields via SendGrid API. Why isn't this working?通过 SendGrid API 更新 SendGrid 联系人自定义字段。为什么这不起作用?
【发布时间】:2021-02-25 23:51:08
【问题描述】:

我正在尝试更新我的 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")```

【问题讨论】:

标签: sendgrid sendgrid-api-v3


【解决方案1】:

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

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

【讨论】:

  • 啊,太棒了,现在无法测试它,但将它扔到我的 Jira 板上改天。谢谢!
猜你喜欢
  • 2022-06-14
  • 1970-01-01
  • 2016-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-20
  • 2016-04-03
相关资源
最近更新 更多