1

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.

Eswar
  • 11
  • 3
  • Looks like you are using a proxy (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/203). Also I found it could be cause by PAT format : https://stackoverflow.com/questions/58991603/azure-devops-203-non-authoritative-information-with-rest-api – jdweng Jun 29 '23 at 08:48
  • Mozilla, where I am using? and after encoding the PAT to base 64 getting the 404 error client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String( System.Text.ASCIIEncoding.ASCII.GetBytes( string.Format("{0}:{1}", "", pat)))); – Eswar Jun 30 '23 at 07:56
  • Error 404 is page is not found. Most likely the token is working. Either your route is not valid (URI) or the route does not allow the user access. See : https://techcommunity.microsoft.com/t5/apps-on-azure-blog/how-to-troubleshoot-azure-functions-http-trigger-404-error/ba-p/3766817#:~:text=One%20of%20the%20common%20causes,result%20in%20a%20404%20error. – jdweng Jun 30 '23 at 08:40

1 Answers1

0

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}");
            }
Eswar
  • 11
  • 3
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jul 02 '23 at 00:26