0
votes

I am trying to register a webhook subscription to notify my application when a work item is edited. I thought I would start by adding a workitem.created webhook. However — I am getting the same error response, whatever subscription I try to register, including the example from the documentation. Here is my example:

POST:

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

Headers:

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

Body:

{
  "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"
  }
}

The error response I get:

{
    "$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
}

I have tried multiple API versions:

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

I have also tried multiple resourceVersions.

I have the following auth token scope: vso.dashboards, vso.identity, vso.notification_manage, vso.work_full, vso.workitemsearch

1

1 Answers

0
votes

This is an issue with Bearer Token using the REST API, and a feedback already submitted to the VSTS team : Create webhook with workitem.created EventType and OAuth token fail

As a workaround, you can use basic Authorization, you can use personal access token or alternate account instead. I tested with below PS sample and that worked for me.

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

Similar thread for your reference : Creating VSTS ServiceHooks (WebHooks) via Rest Api for work item created event fails. Please give a solution


UPDATE:

Authorization: Basic base-64-token-here works on my side. Please make sure you have the correct Base64-encodes there.

Actually you just need to paste the PAT as the password under Authorization tab in Postman, it will automatically encode the token in Postman.

Another way is encode the PAT in other tools (e.g. you can output the $base64AuthInfo from above PS sample ), then copy and paste the base-64-token in the header. Both works for me.

To output the $base64AuthInfo : Write-Host $base64AuthInfo

enter image description here