简体   繁体   English

使用 HTTR POST 创建 Azure Devops 工作项

[英]Using HTTR POST to Create a Azure Devops Work Item

Im attempting to setup some R code to create a new work item task in Azure Devops.我正在尝试设置一些 R 代码以在 Azure Devops 中创建一个新的工作项任务。 Im okay with a mostly empty work item to start with if thats okay to do (my example code is only trying to create a work item with a title).如果可以的话,我可以从一个几乎是空的工作项开始(我的示例代码只是试图创建一个有标题的工作项)。

I receive a 203 response but the work item doesn't appear in Devops.我收到 203 响应,但工作项未出现在 Devops 中。

Ive been following this documentation from Microsoft, I suspect that I might be formatting the body incorrectly.我一直在关注 Microsoft 的这份文档,我怀疑我可能错误地格式化了正文。

https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/create?view=azure-devops-rest-5.1 https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/create?view=azure-devops-rest-5.1

Ive tried updating different fields and formatting the body differently with no success.我尝试更新不同的字段并以不同的方式格式化正文,但没有成功。 I have attempted to create either a bug or feature work item but both return the same 203 response.我试图创建错误或功能工作项,但两者都返回相同的 203 响应。

To validate that my token is working I can GET work item data by ID but the POST continues to return a 203.为了验证我的令牌是否正常工作,我可以通过 ID 获取工作项数据,但 POST 继续返回 203。


require(httr)
require(jsonlite)

url <- 'https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/$bug?api-version=5.1'

headers = c(
    'Authorization' = sprintf('basic %s',token),
    'Content-Type' = 'application/json-patch+json',
    'Host' = 'dev.azure.com'
  )

data <- toJSON(list('body'= list("op"= "add",
                                   "path"= "/fields/System.AreaPath",
                                   "value"= "Sample task")), auto_unbox = TRUE, pretty = TRUE)

res <- httr::POST(url,
                  httr::add_headers(.headers=headers),
                  httr::verbose(),
                  body = data)

Im expecting a 200 response (similar to the example in the link above) and a work item task in Azure DevOps Services when I navigate to the website.当我导航到网站时,我期待 200 响应(类似于上面链接中的示例)和 Azure DevOps Services 中的工作项任务。

Im not the best with R so please be detailed.我不是最好的 R 所以请详细说明。 Thank you in advanced!提前谢谢你!

The POST continues to return a 203. POST继续返回203。

The HTTP response code 203 means Non-Authoritative Information , it should caused by your token format is converted incorrectly. HTTP响应代码203表示“ Non-Authoritative Information ,应由您的令牌格式转换不正确引起。

If you wish to provide the personal access token through an HTTP header, you must first convert it to a Base64 string. 如果希望通过HTTP标头提供个人访问令牌,则必须首先将其转换为Base64字符串。

Refer to this doc described, if you want to use VSTS rest api, you must convert your token to a Base64 string. 参考所描述的文档 ,如果您想使用VSTS rest api,则必须将令牌转换为Base64字符串。 But in your script, you did not have this script to achieve this convert. 但是在您的脚本中,您没有此脚本来实现此转换。

So, please try with the follow script to convert the token to make the key conformant with the requirements(load the base64enc package first): 因此,请尝试使用以下脚本将令牌转换为符合要求的密钥(首先加载base64enc软件包 ):

require(base64enc)
key <- token
keys <- charToRaw(paste0(key,":token"))
auth <- paste0("Basic ",base64encode(keys))

Hope this help you get 200 response code 希望这可以帮助您获得200个响应代码

I know this question is fairly old, but I cannot seem to find a good solution posted yet.我知道这个问题已经很老了,但我似乎还没有找到一个好的解决方案。 So, I will add my solution in case others find themselves in this situation.因此,我将添加我的解决方案,以防其他人遇到这种情况。 Note, this did take some reading through other SO posts and trial-and-error.请注意,这确实需要阅读其他 SO 帖子和反复试验。

Mengdi is correct that you do need to convert your token to a Base64 string. Mengdi 是正确的,您确实需要将令牌转换为 Base64 字符串。

Additionally, Daniel from this SO question pointed out that:此外,来自这个 SO 问题的 Daniel 指出:

In my experience with doing this via other similar mechanisms, you have to include a leading colon on the PAT, before base64 encoding.根据我通过其他类似机制执行此操作的经验,您必须在 PAT 上包含一个前导冒号,在 base64 编码之前。

Mengdi came up big in another SO solution梦迪在另一个 SO 解决方案中大放异彩

Please try with adding [{ }] outside your request body.请尝试在请求正文之外添加 [{ }]。

From there, I just made slight modifications to your headers and data objects.从那里,我只是对您的headersdata对象进行了轻微的修改。 Removed 'body' from your json, and made use of paste to add square brackets as well.从您的 json 中删除'body' ,并使用粘贴添加方括号。 I found that the Rcurl package made base64 encoding a breeze.我发现Rcurl package 使 base64 编码变得轻而易举。 Then I was able to successfully create a blank ticket (just titled) using the API!然后我能够使用 API 成功创建一张空白票证(刚刚命名)! Hope this helps someone!希望这对某人有帮助!

library(httr)
library(jsonlite)
library(RCurl)  

#user and PAT for api
userid <- ''
token= 'whateveryourtokenis'    

url <- 'https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/$bug?api-version=5.1'    

#create a combined user/pat
#user id can actually be a blank string
#RCurl's base64 seemed to work well
c_id <- RCurl::base64(txt = paste0(userid,
                                   ":",
                                   devops_pat 
                                   ),
                       mode = "character"
                      )

#headers for api call
headers <- c(
             "Authorization" = paste("Basic",
                                      c_id,
                                      sep = " "
             ),
             'Content-Type' = 'application/json-patch+json',
             'Host' = 'dev.azure.com' 
             )

#body
data <- paste0("[",
               toJSON(list( "op"= "add",
                            "path"= "/fields/System.Title",
                            "value"= "API test - please ignore"), 
                      auto_unbox = TRUE, 
                      pretty = TRUE
                     ),
                "]"
              )

#make the call
res <- httr::POST(url,
                  httr::add_headers(.headers=headers),
                  httr::verbose(),
                  body = data
                  )

#check status
status <- res$status_code

#check content of response
check <- content(res)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM