Creating VSTS ServiceHooks (WebHooks) via Rest Api

2019-08-24 09:07发布

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条回答
贪生不怕死
2楼-- · 2019-08-24 09:31

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.

查看更多
登录 后发表回答