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

huangapple go评论80阅读模式
英文:

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 request url ---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(responseContent);
Console.WriteLine($"Service hook subscription created. Subscription ID: {createdSubscription.id}");
}

Please help on this.

英文:

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.

答案1

得分: 0

以下是翻译好的代码部分:

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}");
}

希望这有所帮助。

英文:

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}");
            }

huangapple
  • 本文由 发表于 2023年6月29日 08:43:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定