无法通过REST API创建VSTS webhook订阅

时间:2017-12-10 15:56:22

标签: visual-studio tfs azure-devops azure-devops-rest-api

我正在尝试注册webhook订阅,以便在编辑工作项时通知我的应用程序。我想我会先添加一个workitem.created webhook。但是 - 我得到了相同的错误响应,无论我尝试注册什么订阅,包括文档中的示例。这是我的例子:

发表:

https://{my-app}.visualstudio.com/DefaultCollection/_apis/hooks/subscriptions/?api-version=1.0

接头

Authorization:Bearer my-auth-token
Content-Type: application/json

体:

{
  "publisherId": "tfs",
  "eventType": "build.complete",
  "resourceVersion": "1.0-preview.1",
  "consumerId": "webHooks",
  "consumerActionId": "httpRequest",
  "publisherInputs": {
    "buildStatus": "Failed",
    "definitionName": "MyWebSite CI",
    "projectId": "my-project-id"
  },
  "consumerInputs": {
    "url": "https://requestb.in/14xw4741"
  }
}

我得到的错误回复:

{
    "$id": "1",
    "innerException": null,
    "message": "TF400898: An Internal Error Occurred. Activity Id: ecb81b36-4a77-4ae8-9d13-1a5dbb473c8a.",
    "typeName": "System.Exception, mscorlib",
    "typeKey": "Exception",
    "errorCode": 0,
    "eventId": 0
}

我尝试了多个API版本:

api-version=4.1-preview
api-version=1.0
api-version=2.0
api-version=3.0

我还尝试了多个resourceVersion s。

我有以下身份验证令牌范围:vso.dashboardsvso.identityvso.notification_managevso.work_fullvso.workitemsearch

1 个答案:

答案 0 :(得分:0)

这是使用REST API的Bearer Token的问题,以及已经提交给VSTS团队的反馈:Create webhook with workitem.created EventType and OAuth token fail

作为一种解决方法,您可以使用基本授权,您可以使用个人访问令牌或替代帐户。我使用以下PS示例进行测试,这对我有用。

e.g:

Param(
   [string]$vsoAccount = "YouVsoAccount",
   [string]$keepForever = "true",
   [string]$user = "xxxx",
   [string]$token = "qwdiqwaey4aosdgbyqhrdpnvwitguqe2kqllcoc46vvvrxxxkiruq"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))


function CreateJsonBody
{

    $value = @"

{"consumerActionId":"httpRequest","consumerId":"webHooks","consumerInputs":{"url":"http://xxxx"},"eventType":"workitem.updated","publisherId":"tfs","publisherInputs":{"areaPath":"\\TFVC\\","workItemType":"","changedFields":"","projectId":"0142146b-3ee9-495f-8ef0-e8fe223d6571"},"resourceVersion":"1.0","scope":1}

"@

 return $value
}

$json = CreateJsonBody

$uri = "https://$($vsoAccount).visualstudio.com/DefaultCollection/_apis/hooks/subscriptions?api-version=1.0"
$result = Invoke-RestMethod -Uri $uri -Method Post -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

Write-Host $ base64AuthInfo

类似的帖子供您参考:Creating VSTS ServiceHooks (WebHooks) via Rest Api for work item created event fails. Please give a solution

<强>更新

Authorization: Basic base-64-token-here在我这边工作。请确保您在那里有正确的Base64-encodes

实际上你只需将PAT作为密码粘贴在邮递员的Authorization标签下,它就会自动对邮递员中的令牌进行编码。

另一种方法是在其他工具中对PAT进行编码(例如,您可以从PS示例上方输出$base64AuthInfo),然后将base-64-token复制并粘贴到标题中。两者都适合我。

输出$ base64AuthInfo:Write-Host $base64AuthInfo

enter image description here

相关问题