1
votes

In PowerShell, I was able to log in using HTTP Basic authentication For Mail Chimp.

$acctname = 'thisismyusername'
$password = 'thisismyapikey' 
$params = @{
    Uri = 'https://us14.api.mailchimp.com/3.0/';
    Method = 'Get'; #(or POST, or whatever)
    Headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($acctname):$($password)"));} #end headers hash table
} #end $params hash table

$var = Invoke-RestMethod @params
$var

When I try to get basic info on list thats id is "d3a7a4432d".

$URL = "https://us14.api.mailchimp.com/3.0/"
$Endpoint = "/lists/d3a7a4432d"
$URLMailChimp = "$URL$Endpoint"
$gist = Invoke-RestMethod -Method Get -Uri $URLMailChimp

I get this error message:

Invoke-RestMethod : {
    "type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
    "title":"API Key Missing",
    "status":401,
    "detail":"Your request did not include an API key.",
    "instance":""
}
At line:7 char:9
+ $gist = Invoke-RestMethod -Method Get -Uri $URLMailChimp
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
     + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

I don't understand how to pass it my API key again. I thought by logging it it solved the issue.

1

1 Answers

1
votes

I don't use MailChimp, but unless the first invocation provides you with an access token (and the documentation as well as your error message don't look like it would) you need to provide the authentication header with every request.

$acctname = 'thisismyusername'
$password = 'thisismyapikey'
$URL      = 'https://us14.api.mailchimp.com/3.0/'
$listID   = 'd3a7a4432d'

$auth = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${acctname}:${password}"))}

$gist = Invoke-RestMethod -Method Get -Uri "$URL/lists/$listID" -Headers $auth