我正在尝试使用 PowerShell 访问 OpenProvider 的 API,但似乎无法通过身份验证。
API 的文档在这里:https://support.openprovider.eu/hc/en-us/articles/360025683173-Getting-started-with-Openprovider-API
我的代码如下所示:
$EndPoint = "https://api.openprovider.eu/v1beta/auth/login"
function Get-ConfHeaders
{
##Configure headers
$Headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$Headers.Add("ip","0.0.0.0")
$Headers.Add("username","myusername")
$Headers.Add("hash","APIpasswordhashgoeshere")
return $Headers
}
$header = Get-ConfHeaders
Invoke-RestMethod -Method Post -Uri $EndPoint -Headers $header
得到的回应是:
Invoke-RestMethod : The remote server returned an error: (500) Internal Server Error.
At line:36 char:1
+ Invoke-RestMethod -Method Post -Uri $EndPoint -Headers $header
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
我绝不是 API 方面的专家,任何帮助将不胜感激。
好吧,我认为这里的 API 文档还有很多不足之处。
您需要在正文中包含身份验证,并且需要将其转换为 JSON 格式。所以工作代码如下所示:-
$EndPoint = "https://api.openprovider.eu/v1beta/auth/login"
function Get-ConfHeaders
{
##Configure headers
$Headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$Headers.Add("ip","0.0.0.0")
$Headers.Add("username","username")
$Headers.Add("password","passwordhere")
return $Headers
}
$header = Get-ConfHeaders | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri $EndPoint -body $header -ContentType 'application/json'
谢谢大家的帮助。
以下是我如何从您的 uri 获取有效失败信息:
function GetApiSession () {
$username = "username"
$userpass = "password"
$auth = $username + ":" + $userpass
$Encoded = [System.Text.Encoding]::UTF8.GetBytes($auth)
$EncodedPassword = [System.Convert]::ToBase64String($Encoded)
$Global:headers= @{"Authorization"="Basic $($EncodedPassword)"}
try {
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
} catch {
}
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
$Invoke_RestMethod_param = @{
Uri = "https://api.openprovider.eu/v1beta/auth/login"
Method = 'POST'
ContentType = 'application/json'
Headers = $headers
}
$Uri_Response = Invoke-RestMethod @Invoke_RestMethod_param
return $Uri_Response
}
GetApiSession
API 并不是万能的,但这是我发现的处理它们的最佳方式,或者至少是最好的起点。随着时间的推移,我从许多解决方案中积累了这一点,但有时我仍然会遇到麻烦。
当我运行这个时,我无法获得输出,因为我显然没有真正的信用,但我确实得到了真正的响应:
Invoke-RestMethod : The remote server returned an error: (500) Internal Server Error.
我知道这是因为如果我将动词从 POST 更改为 GET,我仍然会从 uri 得到合法的响应:
Invoke-RestMethod : {"desc":"Method is not implemented","code":81}
这就是我开始使用任何新 api 时使用的模式。