Invalid Mailchimp API Key -- wrong datacenter

Sep 2, 2010 at 11:15 PM

Hi all,

Just got a new MailChimp account, and a fresh API key.

When I try to execute lists() I'm getting the following error:

Error Code: 104, Message: Invalid Mailchimp API Key: snip-us2 . You are accessing the wrong datacenter - your client library may not properly support our datacenter mapping scheme.} PerceptiveMCAPI.Types.Api_Error

I found a few people on the Google with the same issue, but no solutions. I tried with another key, but no love there either. Any ideas? Thanks!

Code:

  string apikey = "snip-us2";

  listsParms lp = new listsParms(apikey);

  listsInput li = new listsInput(lp);

  lists lsts = new lists(li);

  listsOutput outpt = lsts.Execute();

 

Web.config:

<section name="MailChimpAPIconfig" type="PerceptiveMCAPI.MCAPISettings, PerceptiveMCAPI"/> [...]

<MailChimpAPIconfig><MCAPI SecureAccess="False" AccessType="Serial" OutputType="JSON" MethodType="POST" Validate="False" Timeout="300000" campaign_authenticate="True" apikey="snip-us2" listUnsubscribe_delete_member="True"       listUnsubscribe_send_goodbye="False" listSubscribe_double_optin="False" /></MailChimpAPIconfig>

Sep 3, 2010 at 1:47 PM
Edited Sep 3, 2010 at 2:05 PM

I came across the same issue, the cause is the wrapper will connect to Datacenter us1 and not to us2, resulting in the error you receive

 In the code there is

 

      [ConfigurationProperty( "DataCenter", DefaultValue = "us1" )]
      internal string DataCenter
      {
         get
         {
            return this["DataCenter"] as string;
         }
      }
      // ------------------------------------------------------------------- general
      [ConfigurationProperty( "apikey", DefaultValue = "" )]
      internal string apikey
      {
         get
         {
            return this["apikey"] as string;
         }
      }
      #endregion

 

as you can see the datacenter will always be "us1" if not specified.

There are two possible solution to your problem

1) add DataCenter="us2" in your web.config tag eg

   <MailChimpAPIconfig>
      <MCAPI
             SecureAccess="False"
             Validate="False"
             DataCenter="us2"
             apikey="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-us2"
        />
  </MailChimpAPIconfig>

Or you can change the source code so that the datacenter is automatically set based on the last three letters of the api key.

 

Coordinator
Sep 3, 2010 at 2:03 PM

Hi guys -- Sorry, about the trouble. 

I meant to post a discussion on this topic earlier when I got a private email about it. Here is the text of my email reply to another person with this issue...


You're not doing anything wrong -- but there is one more thing you need to do because you are not in the 'default' datacenter of us1.
I need to make that clearer in the docs. If I ever get a chance to work on my own site again (hopefully next month) there will be better documentation there.
Anyway...

The DataCenter default value can be changed by making a config specification; in your web.config use something like the following entry:

<configuration>
   <configSections>
      <section name="MailChimpAPIconfig"
type="PerceptiveMCAPI.MCAPISettings, PerceptiveMCAPI" />
	[ ... other entries ....]
   </configSections>

   <MailChimpAPIconfig>
      <MCAPI 
         apikey="yourapikey-us2"
	   DataCenter="us2"
         AccessType="Serial"
         OutputType="JSON"
         MethodType="POST"
         campaign_authenticate="True"
        />
   </MailChimpAPIconfig>


 

Coordinator
Sep 3, 2010 at 2:12 PM

Thanks Steven -- you got your reply in ahead of me while I got intererupted with the phone -- and you were right on the money.

 

Cheers.

David

Sep 27, 2010 at 3:45 PM

I have a single asp.net/vb.net application that accesses different lists on MailChimp based on a dropdown selection. I recently added a list and the API key ends in us2. All of my previous keys ended with us1. I tried changing the config file as noted.

<MailChimpAPIconfig>
      <MCAPI
         apikey="yourapikey-us2"
   DataCenter="us2"
         AccessType="Serial"
         OutputType="JSON"
         MethodType="POST"
         campaign_authenticate="True"
        />
   </MailChimpAPIconfig>
 This works in the sense that the new list is functional. However, the old lists stopped working. This makes perfect sense. I am just trying to figure out to change the datacenter more dynamically. I found the property api_DataCenter in PerceptiveMCAPI.MCAPISettings ; however, it is read-only. What can I do to get access to two different datacenters within one application?

Thanks, 

Jack

Coordinator
Sep 27, 2010 at 5:03 PM
Edited Sep 27, 2010 at 5:04 PM

Yea, while it makes sense (at least at the time before MC starting assigning the second data center) it's a pain in a multi-account situation -- as I found out for myself.

I'm addressing the issue in the v.next release, just not sure how yet as I don't like either idea I came up with for a long-term solution.
Except allowing for external .config files, and providing the ability to change .config files on a request-by-request basis -- but there are some considerations in doing that as well, like I probably won't have time to do it before I want to put the other changes out in v.next.

As an interim hack, if you want to modify the wrapper source, you could do a string.Replace pulling the apikey '-usX' value and use that value to replace the datacenter 'usX.' portion of the url, in the ProcessSerialRequest function (if you're using serial) -- or change the Api_BaseInput class to allow the modification there -- otherwise you'll have to do something similar for each method call in your application -- which is pretty ugly too.

That's the only options that come quickly to mind.

David

 

Coordinator
Sep 27, 2010 at 11:20 PM

Duh.

Well you know, the change for this issue WILL be in V.Next -- not sure why I was thinking it was a big issue to actually have a separate data center code anymore.

It was during the transition, but now it's kind of a non-issue -- the  apikey '-usX' and data center 'usX.' have to be the same per the MC docs.

I'm going to auto-plug the DC from the apikey in v.next -- and not worry about it unless they change the requirements.

dbm

 

Sep 28, 2010 at 12:52 PM

Thanks for all of the information. I decided to go the route of modifying the Api_BaseInput class to include the DataCenter property. I then updated the apikey and URL when they were not us1. Everything works great.

Jack Hoffman

Programmer/Analyst

Pennsylvania Medical Society

777 E Park Dr

Harrisburg, PA 17111

jhoffman@pamedsoc.org

717.909-2616