Estoy jugando con una API RESTful y utilizando el complemento RESTClient de Firefox todo está bien. Puedo consultar fácilmente la API.Powershell Invoke-RestMethod con certificados autofirmados y autenticación básica: ¿algún ejemplo?
Sin embargo, cuando intento conectar la misma llamada a la API en powershell, ¡no funciona!
He intentado el siguiente código de varios otros mensajes que debería descartar la expedición de certificados, pero todavía no puedo conseguir que esto funcione:
# This nugget should help me to get around any self-signed certificate issues I believe
$netAssembly =[Reflection.Assembly]::GetAssembly([System.Net.Configuration.SettingsSection])
if($netAssembly)
{
$bindingFlags = [Reflection.BindingFlags] "Static,GetProperty,NonPublic"
$settingsType = $netAssembly.GetType("System.Net.Configuration.SettingsSectionInternal")
$instance = $settingsType.InvokeMember("Section", $bindingFlags, $null, $null, @())
if($instance)
{
$bindingFlags = "NonPublic","Instance"
$useUnsafeHeaderParsingField = $settingsType.GetField("useUnsafeHeaderParsing", $bindingFlags)
if($useUnsafeHeaderParsingField)
{
$useUnsafeHeaderParsingField.SetValue($instance, $true)
}
}
}
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$headers = @{"AUTHORIZATION"="Basic YWRtaW46Y2xvdWQ="}
# I exported this certificate from the web browser
$cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("HPCSA.crt")
Invoke-RestMethod -Uri https://192.168.5.3:8444/csa/rest/login/CSA-Provider/admin -Certificate $cert -Headers $headers -Method Get`
no soy capaz de replicar esta utilizando la invocación de Powershell V3 -RestMethod y me preguntaba si alguien podría compartir código de muestra para acceder a una API de descanso HTTPS que tiene un certificado autofirmado y también utiliza autorización básica.
mensaje de error me sale:
PS C:\Users\landg> C:\Users\landg\Documents\Scripts\CSA API\CSA_API_DEMO_take2.ps1
Invoke-RestMethod : Unable to connect to the remote server
At C:\Users\landg\Documents\Scripts\CSA API\CSA_API_DEMO_take2.ps1:31 char:1
+ Invoke-RestMethod -Uri https://192.168.5.3:8444/csa/rest/login/CSA-Provider/admi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Sólo una nota para otros usuarios, tenga en cuenta que esto afecta a todos IDontCarePolicy el tráfico de red de su sesión de PowerShell, por lo que una vez que establezca la Política de certificado en eso, ** nada ** que haga a partir de ese momento comprobará la validez de los certificados SSL. – Jaykul