0

I'm trying to create a Mailchimp list using Mailchimps API (v3) and the REST Client gem. I've got this working correctly for retrieving list names and ids. However, I'm getting a 401 Unauthorized - API Key Missing response for the creation of a list. I think my post request is malformed but I'm having trouble indentifying where I'm going wrong. My code looks like this:

params_hash = {
  name: "#{territory}",
  contact: {
    company: "Company",
    address1: "Address",
    city: "City",
    state: "State",
    zip: "0000",
    country: "US"
  },
  permission_reminder: "You are receiving this email because....",
  campaign_defaults: {
    from_name: "From Name",
    from_email: "contact@contact.com",
    subject: "Subject",
    language: "en"
  },
  notify_on_subscribe: "contact1@contact.com",
  notify_on_unsubscribe: "contact1@contact.com",
  email_type_option: true,
  apikey: mailchimp_key
}

RestClient.post("#{mailchimp_url}/lists", { params: params_hash }) { |response, request, result, &block|

}
Thomas Taylor
  • 864
  • 8
  • 25

1 Answers1

1

You should not pass your API key in the payload, instead, you should use HTTP Basic Authentication. RestClient does support this, but it's kind of awkward. If you want to use the RestClient shortcuts, you can modify your URL to include the username/password, like this: https://username:api_key@us1.api.mailchimp.com -- username doesn't matter to MailChimp, but it's required for Basic Auth, so you can pass anything.

Alternately, you can use RestClient's Request class directly, making your request something like this instead:

RestClient::Request.execute(method: :post, url: "#{mailchimp_url}/lists", payload: params_hash, user: 'anything', password: mailchimp_key)

But, honestly? RestClient is not great. I prefer HTTParty, which allows you to create very lightweight wrappers with lots of defaults set for you, OR use it like RestClient, but with a more friendly API:

HTTParty.post("#{mailchimp_url}/lists", body: params_hash, basic_auth: {username: 'whatever', password: mailchimp_key})
TooMuchPete
  • 4,583
  • 2
  • 17
  • 21
  • Thanks for the response. I agree that RestClient is Awkward. However, I find it weird setting the username to anything - it could be confusing for other developers working on the project. I manage to get it working using the following: `RestClient.post("#{mailchimp_url}/lists", params_hash, {:Authorization => "apikey #{mailchimp_key}"})`. Is there anything wrong with that do you think? – Thomas Taylor Dec 12 '16 at 00:45
  • You're right HTTParty is much better than RestClient. Thanks for the help. – Thomas Taylor Dec 13 '16 at 00:29
  • I see what you're saying, yeah. That format is presently supported by MC but it's very custom. It's unlikely to make a difference, I just tend to think that basic auth is easier to read than a custom header, so I do that, but it's personal preference. Also, I typically set the username to the actual username of the account, but that's just my habit not a requirement. – TooMuchPete Dec 13 '16 at 02:01
  • Also: depending on your basic auth client, you might not have to pass a username at all, and it may just send a blank username, which ought to work just as well for MC. – TooMuchPete Dec 13 '16 at 02:02
  • I'm in the process of switching to HTTParty - you're right basic_auth is much easier to read. Also all of the requests to the API follow the same pattern now - much better! I found the following by you: https://github.com/mailchimp/APIv3-examples/pull/12/files where you set the api key as the username. I've done this also and just added comments which should be clear for anyone else working with the implementation. Thanks again! – Thomas Taylor Dec 13 '16 at 02:09