【问题标题】:Cannot create a VSTS webhook subscription via the REST API无法通过 REST API 创建 VSTS Webhook 订阅
【发布时间】:2018-05-24 05:55:45
【问题描述】:

我正在尝试注册 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

我也试过多个resourceVersions。

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

【问题讨论】:

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


    【解决方案1】:

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

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

    例如:

    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)}
    

    写入主机 $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

    其实你只需要在Postman的Authorization标签下粘贴PAT作为密码,它会在Postman中自动对token进行编码。

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

    要输出 $base64AuthInfo :Write-Host $base64AuthInfo

    【讨论】:

    • 为回复干杯...我刚刚得到一个登录页面...只是为了确认,我正在将我的 oAuth 访问令牌 (PAT) 编码为 Base64 字符串并添加标题:@ 987654335@ 我们可以尝试保持代码与语言无关吗?我没有使用 C#——所以让我们谈谈 REST API :) 完整回复:https://developercommunity.visualstudio.com/content/problem/149872/create-webhook-with-workitemcreated-eventtype-and.html
    • @jpsear Authorization: Basic base-64-token-here 在我身边工作。请确保您有正确的Base64-encodes。实际上,您只需将 PAT 作为密码粘贴到 Postman 中的Authorization 选项卡下,它会自动在 Postman 中对令牌进行编码。另一种方法是在其他工具中对令牌进行编码(例如,您可以从 PS 示例中输出 $base64AuthInfo),然后将 base-64-token 复制并粘贴到标题中。两者都对我有用。有关详细信息,请参阅更新的答案。
    • 好的——我想我现在正在关注你,PAT 实际上与 JWT 不同。我将 Base64 编码的 JWT 粘贴为基本身份验证令牌。我已经设法使用从用户帐户生成的 PAT 使其工作。然而——这根本不理想。我可以至少以编程方式生成 PAT 吗?我不希望用户必须管理令牌等...
    • @jpsear 您的要求是通过 REST API 创建 webhook 订阅,这可以通过上面答案中发布的 PS 示例来实现。无论采取何种行动,它都需要凭证(令牌或密码)。因此,您需要先创建用户/密码或令牌。一旦为用户创建了 PAT,就可以直接使用令牌。但是为什么要以编程方式创建 PAT(个人访问令牌)?也许这篇文章有助于理解:domstamand.com/accessing-tfs-2017-programmatically
    • @jpsear 什么是智威汤逊?不管怎样,请看这篇文章来使用 PAT:Authenticate access with personal access tokens for VSTS and TFS
    猜你喜欢
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2020-03-30
    相关资源
    最近更新 更多