如何通过 REST API 创建任务或用户故事

发布于 2025-01-10 20:34:22 字数 1393 浏览 0 评论 0原文

我正在尝试使用此 RESTapi 有多个积压订单。

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

我的代码

function Set-pbiStuff {
    param
    ( 
        [Parameter(Mandatory = $true)] [string] $Organization,
        [Parameter(Mandatory = $true)] [string] $Project,
        [Parameter(Mandatory = $true)] [hashtable] $Token
    )
  
    $Base = "https://dev.azure.com/$($organization)/$($project)/_apis/wit/workitems"
    $workItemType = 'task'
    $URL = "$($Base)/$($workItemType)?api-version=6.0"
    $Json = @(
        @{
            op    = 'add'
            path  = '/fields/System.Title'
            value = $workItemType
        }
    )
    
    $Body = (ConvertTo-Json $Json)
  
    $response = Invoke-RestMethod `
        -Method Post `
        -Uri $URL `
        -ContentType 'application/json' `
        -Body $Body `
        -Headers $Token
    Write-Host $URL
    Write-Host $response
}

$Token= @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($env:SYSTEM_ACCESSTOKEN)")) }

$tt = Set-pbiStuff -Organization 'myOrganization' -Project 'myProject' -Token $Token

return $tt

但我得到的响应是找不到该页面。 我错过了什么?

I am trying to create a user story and task in Azure DevOps with this RESTapi
There are multiple backlogs.

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

My code

function Set-pbiStuff {
    param
    ( 
        [Parameter(Mandatory = $true)] [string] $Organization,
        [Parameter(Mandatory = $true)] [string] $Project,
        [Parameter(Mandatory = $true)] [hashtable] $Token
    )
  
    $Base = "https://dev.azure.com/$($organization)/$($project)/_apis/wit/workitems"
    $workItemType = 'task'
    $URL = "$($Base)/$($workItemType)?api-version=6.0"
    $Json = @(
        @{
            op    = 'add'
            path  = '/fields/System.Title'
            value = $workItemType
        }
    )
    
    $Body = (ConvertTo-Json $Json)
  
    $response = Invoke-RestMethod `
        -Method Post `
        -Uri $URL `
        -ContentType 'application/json' `
        -Body $Body `
        -Headers $Token
    Write-Host $URL
    Write-Host $response
}

$Token= @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($env:SYSTEM_ACCESSTOKEN)")) }

$tt = Set-pbiStuff -Organization 'myOrganization' -Project 'myProject' -Token $Token

return $tt

But the response I get is that the page was not found.
What have I missed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的旅程 2025-01-17 20:34:22

问题是脚本中的类型 ${type} 不正确,缺少字符 $

请尝试这个:

$URL = "$($Base)/"+"$"+"$($workItemType)?api-version=6.0"

内容类型应该是“application/json-patch+json

下面的脚本对我有用:(用于测试的PAT,您可以将其更改回SYSTEM_ACCESSTOKEN< /code> 如果你在管道中运行它)

function Set-pbiStuff {
    param
    ( 
        [Parameter(Mandatory = $true)] [string] $Organization ,
        [Parameter(Mandatory = $true)] [string] $Project ,
        [Parameter(Mandatory = $true)] [string] $Token
    )
  
    $Base = "https://dev.azure.com/$($organization)/$($project)/_apis/wit/workitems"
    $workItemType = 'task'
    $URL = "$($Base)/"+"$"+"$($workItemType)?api-version=6.0"
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f '',$Token)))

    $Json = @(
        @{
            op    = 'add'
            path  = '/fields/System.Title'
            value = $workItemType
        }
    )
    
    $Body = (ConvertTo-Json $Json)
  
    $response = Invoke-RestMethod `
        -Method Post `
        -Uri $URL `
        -ContentType 'application/json-patch+json' `
        -Body $Body `
        -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    Write-Host $URL
    Write-Host $response
}

$tt = Set-pbiStuff -Organization 'orgname' -Project 'ProjectName' -Token Tokenhere

return $tt

The problem is that the type ${type} is incorrect in your script, the character $ is missing.

Please try this:

$URL = "$($Base)/"+"
quot;+"$($workItemType)?api-version=6.0"

And the content type should be "application/json-patch+json"

Below script works for me : (PAT used for the test, you can change it back to SYSTEM_ACCESSTOKEN if you run it in pipeline)

function Set-pbiStuff {
    param
    ( 
        [Parameter(Mandatory = $true)] [string] $Organization ,
        [Parameter(Mandatory = $true)] [string] $Project ,
        [Parameter(Mandatory = $true)] [string] $Token
    )
  
    $Base = "https://dev.azure.com/$($organization)/$($project)/_apis/wit/workitems"
    $workItemType = 'task'
    $URL = "$($Base)/"+"
quot;+"$($workItemType)?api-version=6.0"
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f '',$Token)))

    $Json = @(
        @{
            op    = 'add'
            path  = '/fields/System.Title'
            value = $workItemType
        }
    )
    
    $Body = (ConvertTo-Json $Json)
  
    $response = Invoke-RestMethod `
        -Method Post `
        -Uri $URL `
        -ContentType 'application/json-patch+json' `
        -Body $Body `
        -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    Write-Host $URL
    Write-Host $response
}

$tt = Set-pbiStuff -Organization 'orgname' -Project 'ProjectName' -Token Tokenhere

return $tt
夜雨飘雪 2025-01-17 20:34:22

您尝试使用 PATCH 动词调用 API,请参阅第二行:

$response = Invoke-RestMethod `
    -Method Patch `
    -Uri $URL `
    -ContentType 'application/json' `
    -Body $Body `
    -Headers $AzAuthHeader

API 端点是,如文档所示和您在问题中所述的 POST 端点。

You're trying to call the API with a PATCH verb, see line two:

$response = Invoke-RestMethod `
    -Method Patch `
    -Uri $URL `
    -ContentType 'application/json' `
    -Body $Body `
    -Headers $AzAuthHeader

The API endpoint is, like the documentation shows and you stated in your question, a POST endpoint.

够钟 2025-01-17 20:34:22

尝试此 URL:

$URL = "$($Base)/`$($workItemType)?api-version=6.0"

检查此示例:https://arindamhazra.com/create -azure-devops-task-using-powershell/

Try this URL:

$URL = "$($Base)/`$($workItemType)?api-version=6.0"

Check this sample: https://arindamhazra.com/create-azure-devops-task-using-powershell/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文