Search code examples
asp.net-mvcmailchimprestsharpmailchimp-api-v3.0

RestSharp / MailChimp 'API key missing' error


When I post a campaign on Mailchimp using RestSharp, it tells me my API key is missing, but when I click "Get Campaign", it successfully shows me all the campaign data.

Can anyone tell me where I'm going wrong? Here's my code:

public MailChimpPostModel PostCampaign(MailChimpPostModel post)
{
    var auth = _userBusinessObject.GetUserWebsiteAuthorizationByWebsite(_userId, 
                                                                   _websiteId,
                                                                   _linkvanaNetworkSiteId);
    ApiBaseUrl = <url> ;
    if (auth == null)
        throw new RestRequestResponseException { Error = RestErrorsEnum.NotAuthenticated };

    var request = new RestRequest(3.0/campaigns, Method.POST);
    request.AddParameter("access_token", <Token>);
    request.AddParameter("apikey", <Token> + "-" + <dc>);
    request.AddHeader("content-type", "application/json");
    request.AddBody(post);
    var response = Execute<MailChimpPostModel>(request);
    return response;
}

Solution

  • // replace usX to match the last 3 of your API
    var client = new RestClient("https://usX.api.mailchimp.com/3.0/");
    client.Authenticator = new HttpBasicAuthenticator("user", APIKey);