簡體   English   中英

Rest API 調用適用於 curl,但不適用於 PowerShell

[英]Rest API call works with curl but doesn't work with PowerShell

我有以下有效的curl請求

curl.exe -X GET "https://host.crm.dynamics.com/api/data/v9.1/accounts?$select=name" -H "Authorization: Bearer xxxxx"

並想在 PowerShell 中實現它

Invoke-RestMethod "https://host.crm.dynamics.com/api/data/v9.1/accounts?`$select=name" `
                    -Headers @{Authorization = "Bearer xxxxx"} `
                    -Method  Get

我還嘗試使用Invoke-WebRequest以及 Powershell 中的curl版本進行請求,但沒有奏效

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At E:\Repos\myrepo\host\connection_test.ps1:51 char:1
+ Invoke-RestMethod $hostName `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

使用 Invoke-RestMethod 建立連接時,您要確保 URI 接受客戶端使用的加密。

默認情況下,PowerShell 對 Web 請求使用 TLS 1.0,但您連接到的 URI 可能使用 1.1 或 1.2。 您可以使用以下命令強制使用 TLS v1.1 或 v1.2。

[Net.ServicePointManager]::SecurityProtocol = `
    [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12;
Invoke-RestMethod "https://host.crm.dynamics.com/api/data/v9.1/accounts?`$select=name" `
                -Headers @{Authorization = "Bearer xxxxx"} `
                -Method  Get

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM