【问题标题】:Creating VSTS ServiceHooks (WebHooks) via Rest Api for work item created event fails. Please give a solution通过 Rest Api 为工作项创建事件创建 VSTS ServiceHooks (WebHooks) 失败。请给出解决方案
【发布时间】:2018-04-23 09:22:36
【问题描述】:

我的 API 请求是

网址:https://kogul-ceymplon.visualstudio.com/_apis/hooks/subscriptions?api-version=4.1-preview

方法:POST

内容:“应用程序/json”的“ {\"PublisherId\":\"tfs\",\"EventType\":\"workitem.created\",\"ResourceVersion\":\"1.0-preview.1\",\"ConsumerId\":\ "webHooks\",\"ConsumerActionId\":\"httpRequest\",\"PublisherInputs\":{\"ProjectId\":\"d028a77b-50c4-4bdc-943d-6b072799b884\"},\"ConsumerInputs\" :{\"网址\":\"https://myservice/newreceiver\"}}"

标题:Bearer {accesToken}

我的回复如下
{"$id":"1","innerException":null,"message":"TF400898:发生内部错误。活动 ID:31deba9c-369c-4a31-9be3-67af8ce6249e。","typeName":"系统。异常,mscorlib","typeKey":"Exception","errorCode":0,"eventId":0}

代码:

'using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(
                        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                    ServiceHook hook = new ServiceHook();
                    hook.PublisherId = "tfs";
                    hook.EventType = "workitem.created";
                    hook.ResourceVersion = "1.0-preview.1";
                    hook.ConsumerId = "webHooks";
                    hook.ConsumerActionId = "httpRequest";
                    hook.PublisherInputs = new PublisherInput
                    {
                        projectId = "d028a77b-50c4-4bdc-943d-6b072799b884"
                    };
                    hook.ConsumerInputs = new ConsumerInput
                    {
                        url= "https://myservice/newreceiver"
                    };
                    var javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                    string jsonString = javaScriptSerializer.Serialize(hook);

                    var request = new HttpRequestMessage(HttpMethod.Post, "https://kogul-ceymplon.visualstudio.com/_apis/hooks/subscriptions?api-version=4.1-preview");
                    request.Content = new StringContent(jsonString, Encoding.UTF8, "application/json");
                    string ccc = request.Content.ReadAsStringAsync().Result;
                    using (HttpResponseMessage response = client.SendAsync(request).Result)
                    {
                        response.EnsureSuccessStatusCode();
                        string responseBody = await response.Content.ReadAsStringAsync();
                        return responseBody;
                    }
                }'

我的问题可能是什么?

【问题讨论】:

  • 您是否在 VSTS 扩展中创建服务 hoot?你如何获得accessToken?详细代码是多少?
  • 这是一个 C# 中的 MVC Web 应用程序。我使用 OAuth 获得访问令牌。
  • @starain-MSFT 代码添加到问题

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


【解决方案1】:

首先,workitem.created EventType的json不正确,需要指定区域和任务(尝试手动创建webhook并检查输入)

json 可能是这样的:

{
    "consumerActionId":"httpRequest",
    "consumerId":"webHooks",
    "consumerInputs":{
        "url":"XXX"
    },
    "eventType":"workitem.created",
    "publisherId":"tfs",
    "publisherInputs":{
        "areaPath":"[area path]",
        "workItemType":"Task",
        "projectId":"[project id]"
    },
    "resourceVersion":"1.0"
}

其次,根据我的测试,我们不能使用 OAuth 令牌创建带有 workitem.created EventType 的 webhook,它会抛出该错误。 (可以使用 build.complete EventType 创建 webhook)您可以改用个人访问令牌或备用帐户。

我在这里提交反馈:Create webhook with workitem.created EventType and OAuth token fail,您可以投票和关注。

【讨论】:

  • 是的,我同意你的看法。为什么我们不能使用带有 OAuth Token 的 workitem.created EventType 创建 webhook?如果是,则响应必须是合适的。为什么响应是internal server error?
  • 我认为那个 REST API 有问题,所以我提交了一个反馈,你可以投票并关注它。
  • 您可以使用个人访问令牌或备用帐户。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-06
  • 1970-01-01
  • 2018-05-24
  • 1970-01-01
  • 2019-01-27
  • 2022-09-30
相关资源
最近更新 更多