Missing API key when making API RestMethod Call After HTTP Basic authentication in Mailchimp

一曲冷凌霜 提交于 2021-01-28 20:43:52

问题


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:


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


来源:https://stackoverflow.com/questions/40026611/missing-api-key-when-making-api-restmethod-call-after-http-basic-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!