简体   繁体   中英

RestAPI authentication with PowerShell

I am trying to access the API of OpenProvider using PowerShell and I can't seem to get past Authentication.

The documentation for the API is here: https://support.openprovider.eu/hc/en-us/articles/360025683173-Getting-started-with-Openprovider-API

And my code looks like this:

$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 

The response get is:

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

I am by no means an expert when it comes to API and any help would be appreciated.

Ok, I think the API documentation here leaves a lot to be desired.

You need to include the authentication in the body and it needs to be converted to JSON format. So the working code looks like this:-

$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' 

Thanks for the help everyone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM