2012-08-29 15 views
7

Gram z RESTful API i za pomocą wtyczki RESTClient RESTClient wszystko jest dobrze. Mogę łatwo zapytać o API.Powershell Invoke-RestMethod przy użyciu certyfikatów z własnym podpisem i podstawowego uwierzytelniania - dowolne przykłady?

Jednak, gdy próbuję podłączyć to samo wywołanie API do powłoki powershell, to nie działa!

Próbowałem następujący kod z różnych innych stanowisk, które powinny wykluczać kwestii certyfikatów, ale nadal nie można uzyskać to do pracy:

# 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` 

nie jestem w stanie powtórzyć to przy użyciu PowerShell V3 powołują -RestMethod i zastanawiał się, czy ktoś mógłby udostępnić przykładowy kod dostępu do interfejsu API HTTPS, który ma samopodpisany certyfikat, a także korzystać z podstawowej autoryzacji.

Komunikat o błędzie pojawia się:

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 

Odpowiedz

10

The source of my solution

Stosując ten dostaje mnie na rzeczywistym serwerem .... teraz z HTTP 500 błędów, ale ten błąd jest na inny dzień.

Oto moja „praca” fragment:

 add-type @" 
     using System.Net; 
     using System.Security.Cryptography.X509Certificates; 

      public class IDontCarePolicy : ICertificatePolicy { 
      public IDontCarePolicy() {} 
      public bool CheckValidationResult(
       ServicePoint sPoint, X509Certificate cert, 
       WebRequest wRequest, int certProb) { 
       return true; 
      } 
     } 
    "@ 
    [System.Net.ServicePointManager]::CertificatePolicy = new-object IDontCarePolicy 

    Invoke-RestMethod -Uri https://192.168.5.3:8444/csa/rest/login/CSA-Provider/admin -Headers @{"AUTHORIZATION"="Basic YWRtaW46Y2xvdWQ="} -Method Get 

Mam nadzieję, że przyczyni się to do kogoś innego z kilku frustrujących godzin :)

+3

Tylko uwaga dla innych użytkowników, należy pamiętać, że to wszystko wpływa IDontCarePolicy ruch sieciowy z sesji PowerShell, więc gdy ustawisz wartość CertificatePolicy na taką, ** nic **, które robisz od tego momentu, sprawdzi ważność certyfikatów SSL. – Jaykul

7
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} 
+0

Nice - robi to samo, co rozwiązanie Grazzer'a, ale w jednej linii. –

Powiązane problemy