通过Rest Api为工作项创建事件创建VSTS ServiceHooks(WebHooks)失败.请给一个解决方案 [英] Creating VSTS ServiceHooks (WebHooks) via Rest Api for work item created event fails. Please give a solution

查看:111
本文介绍了通过Rest Api为工作项创建事件创建VSTS ServiceHooks(WebHooks)失败.请给一个解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的API请求为

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

方法:POST

内容:"的"application/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":空,消息":"TF400898:发生内部错误.活动ID:31deba9c-369c-4a31-9be3-67af8ce6249e.","typeName":系统.异常,mscorlib," typeKey:"异常," 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;
                    }
                }'

我这边有什么毛病?

解决方案

首先,json对于workitem.created EventType不正确,您需要指定区域和任务(尝试手动创建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).您可以改用个人访问令牌或备用帐户.

我在此处提交反馈:使用workitem创建webhook.创建的EventType和OAuth令牌失败,您可以投票并关注.

My API Request is as

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

Method: POST

Content: "application/json" of " {\"PublisherId\":\"tfs\",\"EventType\":\"workitem.created\",\"ResourceVersion\":\"1.0-preview.1\",\"ConsumerId\":\"webHooks\",\"ConsumerActionId\":\"httpRequest\",\"PublisherInputs\":{\"ProjectId\":\"d028a77b-50c4-4bdc-943d-6b072799b884\"},\"ConsumerInputs\":{\"Url\":\"https://myservice/newreceiver\"}}"

Header : Bearer {accesToken}

My Response is as
{"$id":"1","innerException":null,"message":"TF400898: An Internal Error Occurred. Activity Id: 31deba9c-369c-4a31-9be3-67af8ce6249e.","typeName":"System.Exception, mscorlib","typeKey":"Exception","errorCode":0,"eventId":0}

Code:

'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;
                    }
                }'

What could be the fault at my side?

解决方案

First, the json is incorrect for workitem.created EventType, you need to specify area and task (Try to create a webhook manually and check the inputs)

The json could be like this:

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

Secondly, based on my test, we can’t use OAuth token to create a webhook with workitem.created EventType, it will throw that error. (Can create a webhook with build.complete EventType) You can use personal access token or alternate account instead.

I submit a feedback here: Create webhook with workitem.created EventType and OAuth token fail, you can vote and follow.

这篇关于通过Rest Api为工作项创建事件创建VSTS ServiceHooks(WebHooks)失败.请给一个解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆