首页 > 解决方案 > 如何使用 azure devops python api。需要一些示例:获取用户故事信息、创建用户故事、更改状态

问题描述

我有一个完全满足我要求的程序,我在没有使用库的情况下编写它。我不小心删除了文件。使用版本控制。总是。

我正在尝试获取任务列表,但我完全不明白我在做什么。有人有例子吗?我需要几个简单的操作:创建带有标签的用户故事、获取用户故事信息(标题、描述、ID)、更改用户故事状态(活动、关闭)

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v6_0.work_item_tracking.work_item_tracking_client import WorkItemTrackingClient
import pprint

from azure.devops.v6_0.work_item_tracking.models import WorkItemBatchGetRequest
m_token = "x7micezp4c25btn3a1111111111111o23zlxuwrpdoa"

m_url = 'https://dev.azure.com/logistics/'
m_cred = BasicAuthentication('', m_token)
m_conn = Connection(base_url=m_url, creds=m_cred)
m_client = m_conn.clients.get_core_client()



kek = WorkItemBatchGetRequest(ids = [22, 33])
cc = WorkItemTrackingClient(m_url)

c = cc.get_work_items_batch(kek, "Manyport")

print(c.value)

标签: pythonazureazure-devopsdevops

解决方案


感谢汤姆支持您的回答,添加了有关如何在函数应用设置中添加用户身份的屏幕截图。

提供对服务资源的创建、检索、更新或删除访问的 Rest API 也支持 HTTP 操作并且是服务端点。

下面是带有 HTTP 客户端类的 Rest API 的示例代码。

public static async void GetProjects()
{
    try
    {
        var personalaccesstoken = "PAT_FROM_WEBSITE";

        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Add(
                new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                Convert.ToBase64String(
                    System.Text.ASCIIEncoding.ASCII.GetBytes(
                        string.Format("{0}:{1}", "", personalaccesstoken))));

            using (HttpResponseMessage response = await client.GetAsync(
                        "https://dev.azure.com/{organization}/_apis/projects"))
            {
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBody);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

有关更多信息,请查看带有 Rest API 的 Azure DevOps


推荐阅读