使用VSTS API完成拉取请求时,为什么会收到400 - 错误请求

时间:2018-03-08 09:10:47

标签: azure-devops-rest-api

我猜这是一个许可问题。我知道URI和正文等都是正确的,因为它可以在标题中使用不同的授权。

我尝试将_admin / _versioncontrol下的权限调整为'允许',根据创建拉请求时对我有用的内容。参考: Getting a '403 Forbidden' when calling the Pull Request API on VSTS

虽然我可以很好地创建/放弃请求,但我似乎无法自动完成它们。

我错过了什么?

编辑1(基于星号chen-MSFT的请求):

当我在我的powershell脚本中捕获异常并执行 Sub Find_Matches() Dim CompareRange As Variant, x As Variant, y As Variant ' Set CompareRange equal to the range to which you will ' compare the selection. Set CompareRange = Range("C1:C5") ' NOTE: If the compare range is located on another workbook ' or worksheet, use the following syntax. ' Set CompareRange = Workbooks("Book2"). _ ' Worksheets("Sheet2").Range("C1:C5") ' ' Loop through each cell in the selection and compare it to ' each cell in CompareRange. Dim intRow As Integer For Each x In Selection intRow = intRow + 1 For Each y In CompareRange If x = y Then Cells(intRow, 2) = "Yes" Exit For Else Cells(intRow, 2) = "No" End If Next y Next x End Sub 时,我得到了这个:

Write-Output $_.Exception

当我从try-catch块中删除The remote server returned an error: (400) Bad Request. 时,我将其打印到consolve输出:

Invoke-RestMethod

再一次,当我将上面的URI和正文粘贴到Postman时,它完全正常。看到我的Powershell脚本和Postman调用之间的唯一区别是授权标头,这意味着这是一个权限问题,而且仅限于权限问题。

1 个答案:

答案 0 :(得分:2)

您需要指定相同的用户ID(当前授权用户)才能通过REST API(param( [string]$project, [string]$repo, [string]$sourceBranch, [string]$targetBranch, [string]$title, [string]$des, [string]$token ) $bodyObj=@{ "sourceRefName"="refs/heads/$sourceBranch"; "targetRefName"= "refs/heads/$targetBranch"; "title"= "$title"; "description"="$des"; } $bodyJson=$bodyObj| ConvertTo-Json $uri="https://XXX.visualstudio.com/DefaultCollection/$project/_apis/git/repositories/$repo/pullRequests?api-version=3.0" Write-Output $bodyJson Write-Output $uri $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "test",$token))) $result=Invoke-RestMethod -Method POST -Uri $Uri -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $bodyJson $serviceId=$result.createdBy.id Write-Host $serviceId Write-Host "##vso[task.setvariable variable=userId;]$serviceId" )将拉取请求更新为自动完成,否则您将收到400错误。

如果您只想完成拉取请求,可以通过REST API将拉取请求状态更新为“已完成”。

更新

创建拉取请求并获取用户ID的简单代码:

lm