0
votes

Tearing my hair out with this, i'm trying to use the kanboard API (kanboard.org) from powershell, essentially using curl which from investigation is exactly what Invoke-RestMethod is.

I'm getting the following error;

Invoke-RestMethod : Cannot bind parameter 'Headers'. Cannot convert the ""X-API-Auth",
"agBzAG8AbgByAHAAYwA6ADQAMgBjAGQANwAwADEAMgA3ADgAMgAyAGMAYwBiAGUANwA1ADcAMAA3AGYANQBmAGUAZQA2ADkANgBmADYAYQA1AGIAOQAwADIANABiADEAZQBhADAANwBjAGIAOQA1ADQAYQA5AGQAYQA5AGMAYQAwAGYAYgA0AA=="" value of type
"System.String" to type "System.Collections.IDictionary".
At line:1 char:77
+ ... RestMethod -Uri $kanboardserver -Method Post -Headers $header -Body $ ...
+                                                           ~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

My API call code using powershell is this;

$kanboardserver = "http://mykanboardserver/jsonrpc.php"
$kanboardtoken = "jsonrpc:42cd70127822ccbe75707f5fee696f6a5b9024b1ea07cb954a9da9ca0fb4"
$converted = $kanboardtoken | ConvertTo-Base64

$headersKan = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" -ErrorAction Stop
$headersKan.Add("X-API-Auth", $converted)

$payload = @{ jsonrpc = "2.0"; 
            method = "getAllProjects"; 
            id = 1;
            } | ConvertTo-Json

$responseKan = Invoke-RestMethod -Uri $kanboardserver -Method Post -Headers $header -Body $payload -ErrorAction Stop
Write-Host $responseKan

Just for reference this is the documentation example of how to invoke the API using CURL which is what i based my above code on. (https://docs.kanboard.org/en/latest/api/examples.html)

curl \
-u "jsonrpc:19ffd9709d03ce50675c3a43d1c49c1ac207f4bc45f06c5b2701fbdf8929" \
-d '{"jsonrpc": "2.0", "method": "getAllProjects", "id": 1}' \
http://localhost/kanboard/jsonrpc.php
2

2 Answers

0
votes

Updated

You Should use an Hashtable for the headers IDictionary parameter, like this:

Regarding your base64 string, i don't know the ConvertTo-Base64 you are using, anyway this is how i do it:

$kanboardtoken = "jsonrpc:57c9d99c1f5629959d237822a000a20b22913c71688e6520e5638beec11b"
$Base64 = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($kanboardtoken))


$headers = @{
'X-API-Auth' = $Base64
}

$payload = @{ jsonrpc = "2.0"; 
            method = "getAllProjects"; 
            id = 1;
            } | ConvertTo-Json

$responseKan = Invoke-RestMethod -Uri $kanboardserver -Method Post -Headers $headers -Body $payload -ErrorAction Stop
-1
votes

Thanks, So i'm back to an error I have come across before, which is an authorization problem.

Invoke-RestMethod : {"jsonrpc":"2.0","error":{"code":401,"message":"Unauthorized"},"id":null}

I know the token is correct as I have mocked up a php client and the API is working.

The latest code that gives me this error is below, it has to be with the way the header is formed, I can't think of anything else.

 $kanboardserver = "http://mykanboardserver/jsonrpc.php"
    $kanboardtoken = "jsonrpc:57c9d99c1f5629959d237822a000a20b22913c71688e6520e5638beec11b" | ConvertTo-Base64

    $headers = @{
        'X-API-Auth' = $kanboardtoken
        }

    $payload = @{ jsonrpc = "2.0"; 
                method = "getAllProjects"; 
                id = 1;
                } | ConvertTo-Json


        $responseKan = Invoke-RestMethod -Uri $kanboardserver -Method Post -Headers $headers -Body $payload -ErrorAction Stop
        Write-Host $responseKan