如何使用WIQL从RESTful API获取数据,但似乎无法正常工作

时间:2019-05-26 03:52:50

标签: powershell azure-devops azure-devops-rest-api

几天后我一直无法弄清代码的问题。 你们可以看看我说错了吗? 组织名称和项目名称在代码中正确输入。

这是PowerShell ISE上显示的错误消息:

  

Invoke-RestMethod:无法发送具有这种动词类型的内容主体。在   \ Project \ DevopTask.ps1:47 char:17   + ... $ response =调用-RestMethod -Uri $ url -headers $ headers -Method获取...   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~       + CategoryInfo:未指定:(:) [Invoke-RestMethod],ProtocolViolationException       + FullyQualifiedErrorId:System.Net.ProtocolViolationException,Microsoft.PowerShell.Commands.InvokeRestMet   hodCommand

$collectionuri = $Env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI
$token = $Env:SYSTEM_ACCESSTOKEN # need to configure build to allow passing OAuth tokens

$basicAuth = "{0}:{1}" -f '', 'token~~fdsafdsa'
$basicAuth = [System.Text.Encoding]::UTF8.GetBytes(":$($basicAuth)")
$basicAuth = [System.Convert]::ToBase64String($basicAuth)
$headers = @{Authorization=("Basic {0}"-f $basicAuth)}

$WorkItemType = 'Recently updated'

$url = $collectionuri + 'https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/wit/queries/test/wiql?api-version=5.0'

$WIQL_query = "select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.IterationPath] from WorkItems where [System.TeamProject] = @project and [System.Id] in (@recentProjectActivity) and not [System.State] in ('Closed', 'Inactive', 'Completed') and [System.IterationPath] Under 'Sprint number two' order by [System.ChangedDate] desc"
$body = @{ query = $WIQL_query }
$bodyJson=@($body) | ConvertTo-Json

$response = Invoke-RestMethod -Uri $url -headers $headers -Method Get -ContentType "application/json" -Body $bodyJson

$workitems = $response.workItems
Write-Host "Found" $workitems.Count "work items of type:" $WorkItemType

1 个答案:

答案 0 :(得分:0)

您只需要将{{1}中的-MethodInvoke-RestMethod更改为Get

您尝试使用 W ork I tem Q uery L 来获取工作项,在这种情况下您需要在主体中传递WIQL的API,因此Post应该为-Method

请参阅文档here