用这个撕掉我的头发,我试图使用来自powershell的kanboard API(kanboard.org),基本上使用curl,调查中的curl正是Invoke-RestMethod的用途。
我收到以下错误;
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
我使用PowerShell的API调用代码是这样的;
$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
仅供参考,这是如何使用CURL调用API的文档示例,这是我基于上述代码的内容。 (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
答案 0 :(得分:0)
<强>更新强>
您应该使用Hashtable作为标题IDictionary
参数,如下所示:
关于你的base64字符串,我不知道你正在使用的ConvertTo-Base64
,无论如何这就是我的工作方式:
$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 :(得分:-1)
谢谢,所以我回到之前遇到过的错误,这是一个授权问题。
Invoke-RestMethod : {"jsonrpc":"2.0","error":{"code":401,"message":"Unauthorized"},"id":null}
我知道令牌是正确的,因为我已经模拟了一个php客户端并且API正在运行。
下面给出了这个错误的最新代码,它必须与标题的形成方式有关,我无法想到其他任何内容。
$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