Search code examples
c#subscriptionazure-devops-rest-apiazure-service-hooks

Unable to create service hook/webhook subscription in Azure ADO using Rest API C#


I am trying to create a service hook/webhook Subscription for Workitem Created event but it is not working. I am getting error says "StatusCode: 203, ReasonPhrase: 'Non-Authoritative Information". I am using personal access token to authenticate. This token is having full access. I am using below code to create subscription

//urlType is azure function url ---https://function-xxxxxxxx-syncxxxxxxxx-v1.azurewebsites.net/api/CreateWorkItem?code=M4bvxxxxxxxxxxxxxxx

//workItemType is "workitem.created" sample requesturl ---https://dev.azure.com/TestOrg/TestProject/_apis/hooks/subscriptions?api-version=7.0

 var subscription = new
            {
                publisherId = "tfs",
                eventType = workItemType,
                resourceVersion = "1.0",
                consumerId = "webHooks",
                consumerActionId = "httpRequest",
                consumerInputs = new
                {
                    url = urlType
                }
            };


            var subscriptionJson = JsonConvert.SerializeObject(subscription);
            var content = new StringContent(subscriptionJson, System.Text.Encoding.UTF8, "application/json");
            
                requestUri = $"{baseUrl + orga}/{project}/_apis/hooks/subscriptions?api-version=7.0"; 

            using (HttpClient client = new HttpClient())
            {                    
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", personalAccessToken); // personalAccessToken is having full access
                var response = await client.PostAsync(requestUri, content);
                response.EnsureSuccessStatusCode();
                var responseContent = await response.Content.ReadAsStringAsync();
                var createdSubscription = JsonConvert.DeserializeObject<dynamic>(responseContent);
                Console.WriteLine($"Service hook subscription created. Subscription ID: {createdSubscription.id}");
            }

Please help on this.


Solution

  • Fixed the issue

      var subscription = new
                {
                    publisherId = "tfs",
                    eventType = workItemType,
                    resourceVersion = "1.0",
                    consumerId = "webHooks",
                    consumerActionId = "httpRequest",
                    publisherInputs = new
                    {
                        projectId = project.Value
                    },
                    consumerInputs = new
                    {
                        url = urlType
                    }
                };
    
                var subscriptionJson = JsonConvert.SerializeObject(subscription);
                var content = new StringContent(subscriptionJson, System.Text.Encoding.UTF8, "application/json");
                string requestUri = "";
                requestUri = $"{baseUrl + orga}/_apis/hooks/subscriptions?api-version=7.0";
    
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", pat))));
                    var response = await client.PostAsync(requestUri, content);
                    response.EnsureSuccessStatusCode();
                    var responseContent = await response.Content.ReadAsStringAsync();
                    Console.WriteLine($"Service hook subscription: {workItemType} created for project: {project.Key}");
                }