从 Node js 访问 Azure DevOps 时收到 404

问题描述 投票:0回答:1

我有一个 Node JS 应用程序,我想在 Azure DevOps 中创建一个工作项,我生成了具有工作项读写权限的个人访问令牌密钥,如下图所示:

下面是我从 Node JS 访问我的工作项的代码,并且还为此安装了 axios:

const axios = require('axios');

// Azure DevOps organization URL
const orgUrl = 'https://dev.azure.com/<organisationName>';

// Personal Access Token (PAT) with Work Item read/write scope
const pat = ''; // PAT generated with above permissions in devOps

// Project name
const projectName = 'ProjectName';

// Work item type (e.g., 'Task', 'User Story', etc.)
const workItemType = 'Task';

// Work item fields


async function createWorkItem(short_description, description, urgency, impact, assignment_group, caller_id, assigned_to) {
    const workItemData = {
        fields: {
            'System.Title': short_description,
            'System.AreaPath': projectName,
            'System.IterationPath': projectName,
            'Microsoft.VSTS.Common.Priority': urgency,
            'Microsoft.VSTS.Common.State': 'To Do',
            'System.AssignedTo': assigned_to,
            // Add more fields as needed
        },
    };

    try {
        const url = `${orgUrl}/${projectName}/_apis/wit/workitems/${workItemType}?api-version=7.1`;
        console.log("COMPLETE URL: ",url);

        const response = await axios.post(url, [workItemData], {
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Basic ${Buffer.from(`:${pat}`).toString('base64')}`,
            },
        });

        console.log('Work item created successfully:', response.data);
    } catch (error) {
        console.error('Error creating work item:', error.response ? error.response.data : error.message);
    }
}

当我尝试访问 时,出现 404(页面未找到):

我在上面的代码中生成的完整网址: https://dev.azure.com/OrganizationName/ProjectName/_apis/wit/workitems/Task?api-version=7.1

由于我是新手,任何帮助将不胜感激。

javascript node.js azure-devops axios azure-devops-rest-api
1个回答
0
投票

404错误意味着Rest API URL有问题。

请参阅有关 Rest API 的文档来创建工作项:工作项 - 创建

POST https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${type}?api-version=7.1-preview.3

我们需要在工作项类型参数前添加

$

要解决此问题,您需要在工作项类型参数前添加

$
字符。

例如:

const workItemType = '$Task';

完整样本:

const axios = require('axios');

// Azure DevOps organization URL
const orgUrl = 'https://dev.azure.com/<organisationName>';

// Personal Access Token (PAT) with Work Item read/write scope
const pat = ''; // PAT generated with above permissions in devOps

// Project name
const projectName = 'ProjectName';

// Work item type (e.g., 'Task', 'User Story', etc.)
const workItemType = '$Task';

// Work item fields


async function createWorkItem(short_description, description, urgency, impact, assignment_group, caller_id, assigned_to) {
    const workItemData = {
        fields: {
            'System.Title': short_description,
            'System.AreaPath': projectName,
            'System.IterationPath': projectName,
            'Microsoft.VSTS.Common.Priority': urgency,
            'Microsoft.VSTS.Common.State': 'To Do',
            'System.AssignedTo': assigned_to,
            // Add more fields as needed
        },
    };

    try {
        const url = `${orgUrl}/${projectName}/_apis/wit/workitems/${workItemType}?api-version=7.1`;
        console.log("COMPLETE URL: ",url);

        const response = await axios.post(url, [workItemData], {
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Basic ${Buffer.from(`:${pat}`).toString('base64')}`,
            },
        });

        console.log('Work item created successfully:', response.data);
    } catch (error) {
        console.error('Error creating work item:', error.response ? error.response.data : error.message);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.