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

爱⌒轻易说出口 提交于 2019-12-23 05:00:40

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/47217833/creating-vsts-servicehooks-webhooks-via-rest-api-for-work-item-created-event-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!