brandonseydel/MailChimp.Net

OAuth doesn't work under .NET Framework 4.8 and Nuget 5.2

Closed this issue · 1 comments

We encountered this same issue upgrading to .NET Framework 4.8 and using Nuget package 5.2. Essentially, OAuth doesn't work at all and provides the exact error AshleyMedway provided above. We tried downgrading to Nuget package 5.1, which was working under .NET Framework 4.6.1, but we receive the same error as 5.2 in the 4.8 Framework.

The error occurs here when the GetHttpClientFactory tries to reference the _options.ApiKey and it is null because we're using OAuth:

private MailChimpHttpClient FactoryProvidedHttpClient(string resource)
        {           
            var client = GetHttpClientFactory().CreateClient(_options.ApiKey);
            return new MailChimpHttpClient(client, _options, resource);
        }

The workaround by AshleyMedway above "halfway" worked in that it sets the private _apiKey without using the public setter.

But when we tried a customer that has a different DataCenter (us10) rather than what I"m assuming is the default (us12), we got the following error:

Title: API Key Invalid
Type: https://mailchimp.com/developer/marketing/docs/errors/
Status: 401
Instance: ea3af64d-ec77-61a2-aaaf-4349c170ff98
Detail: Your API key may be invalid, or you've attempted to access the wrong datacenter.
Errors:
Request URI:https://us12.api.mailchimp.com/3.0/lists?count=1000&offset=0

So because the _apiKey now has a value, somewhere in the code it must still be trying to get the datacenter from it, even though we set the datacenter in initialization. Our "hack" is to add the datacenter at the end of the "OAuthMode" string and it worked:

        MailChimpOptions mailChimpOptions = new MailChimpOptions();
        mailChimpOptions.OauthToken = oauthToken;
        mailChimpOptions.DataCenter = dataCenter;
        mailChimpOptions.GetType().GetField("_apiKey", BindingFlags.Instance | BindingFlags.NonPublic) ?.SetValue(mailChimpOptions, "OAuthMode-" + dataCenter);
        _mailChimpManager = new MailChimpManager(mailChimpOptions); 

This would be REALLY nice if it was really fixed and we didn't have to rely on such a hack to get OAuth to work.

Thank you.

Originally posted by @SunnysGlimpse in #534 (comment)

5.3.0 has this