英文:
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
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}");
            }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论