在我的应用程序中,我想在创建用户时自动订阅 MailChimp 列表。我找到的所有文档都集中在 MailChip API 2.0 和 Gibbon 1.x 上。
这是我到目前为止所拥有的:
我创建了一个 .env 文件:
MAILCHIMP_API_KEY = "my_api_key"
MAILCHIMP_LIST_ID = "my_list_id"
我创建了一个名为 gibbon.rb 的初始化程序:
Gibbon::Request.api_key = ENV["MAILCHIMP_API_KEY"]
Gibbon::Request.timeout = 15
然后创建了一个作业:
class SubscribeUserToMailingListJob < ActiveJob::Base
queue_as :default
def perform(user)
gibbon = Gibbon::Request.new
# I am getting errors from this line
gibbon.lists(ENV["MAILCHIMP_LIST_ID"]).members.create(body: {email_address: user.email, status: "subscribed", merge_fields: {FNAME: user.name, LNAME: ""}})
end
end
在我的 user.rb 中我检查后有这个
after_create :subscribe_user_to_mailing_list
并在私人下添加了这个:
def subscribe_user_to_mailing_list
SubscribeUserToMailingListJob.perform_later(self)
end
我收到 400 错误gibbon.lists(ENV["MAILCHIMP_LIST_ID"]).members.create(body: {email_address: user.email, status: "subscribed", merge_fields: {FNAME: user.name, LNAME: ""}})
********编辑********
根据这次谈话。
我认为发生错误是因为电子邮件地址已存在于列表中。我第一次遇到错误时,电子邮件地址已添加到 MailChimp 列表中,但由于另一个错误,该用户未在我的应用程序中创建。现在,当我尝试使用相同的电子邮件地址创建用户时,会弹出错误 400,因为该电子邮件已经存在。这也可以防止在我的应用程序中创建用户。
请有更多经验的人验证这是否正确?