Ниже приведен код, который я пытался сделать вызовом API для токена.Похоже, что x-www-form-urlencoded вызывает 401 несанкционированный при преобразовании в тело. Что является стандартным процессом для преобразования тела в x-www-form-urlencoded.
Add-Type @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback
{
public static void Ignore()
{
ServicePointManager.ServerCertificateValidationCallback +=
delegate
(
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
)
{
return true;
};
}
}
"@
[ServerCertificateValidationCallback]::Ignore();
$header = @{
"content-Type"="application/x-www-form-urlencoded"
"accept"="application/json"
}
$APICURL= "myurl"
$json = @{ "grant_type"="access"
"client_id" = "xyz"
"client_secret" = "abc"
"scope" = "one"}
$body = $json | ConvertTo-Json
$response =Invoke-Restmethod -Uri $APICURL -Method Post -Headers $header -Body $body