Azure 工作项 Azure REST API POST 请求 - 503 服务不可用 0x80070057,无效请求

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

我正在尝试使用 python 脚本以编程方式在 Azure 板上创建问题卡。

我正在使用 PAT(个人访问令牌)。

headers = base64encodedPAT
headers[Content-Type']='application/json-patch+json' 
payload = dictionary_of_different_values_to_set

requesturl=f"https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/$Issue?api-version=7.0"
requestcall = requests.post(url=requesturl, headers=headers, data=bytes(json.dumps(payload),'utf-8'))

我尝试了什么:

  • 当我运行 azure pipeline 时,我收到“失败:400 错误请求”
  • 当我在线进行 REST Api 测试时,我收到“503 服务不可用 0x80070057,无效请求”
  • 我参考了微软网站,但没有太多细节
  • 我看到了很多 YouTube 视频,其中展示了 PAT 的创建,但没有介绍如何在管道中使用它。 “工作项目”API 上的视频不多。

请让我知道我的方法有什么问题,或者提出一些想法,或者向我指出好的在线资源。

python azure-devops azure-devops-rest-api tfs-workitem
1个回答
0
投票

我正在尝试使用 python 脚本以编程方式在 Azure 板上创建问题卡。

  1. 使用 工作项 - 创建 REST API 使用 python 脚本在 Azure 板中创建问题工作项。

解决方案 1:对授权标头的

personal_access_token
进行编码。

import requests
import base64

# Replace with your Azure DevOps organization and project details
organization = ''
project = ''
personal_access_token = ''  
work_item_type = 'Issue'  # e.g., Task, Bug

# Encode the PAT for the Authorization header
encoded_pat = base64.b64encode(f':{personal_access_token}'.encode()).decode()
# Define the headers
headers = {
    'Content-Type': 'application/json-patch+json',
    'Authorization': f'Basic {encoded_pat}'
}

# API endpoint for creating a work item
url = f'https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${work_item_type}?api-version=7.1-preview.3'

# Data for the new work item
data = [
    {
        "op": "add",
        "path": "/fields/System.Title",
        "value": "Sample  work item"
    },
    {
        "op": "add",
        "path": "/fields/System.Description",
        "value": "This is a sample work item created via REST API."
    }
]

# Make the request to create the work item
response = requests.post(url, headers=headers, json=data)

if response.status_code == 200:
    work_item = response.json()
    print(f"Work item created: {work_item['id']}, title: {work_item['fields']['System.Title']}")
else:
    # Print the raw response content for debugging
    print(f"Failed to create work item: {response.status_code}")
    print(f"Response content: {response.text}")

解决方案2:直接在

auth=('', personal_access_token)
中使用
requests.post

import requests

# Replace with your Azure DevOps organization and project details
organization = ''
project = ''
personal_access_token = ''  
work_item_type = 'Issue'  # e.g., Task, Bug

# API endpoint for creating a work item
url = f'https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${work_item_type}?api-version=7.0'

# Data for the new work item
data = [
    {
        "op": "add",
        "path": "/fields/System.Title",
        "value": "Sample  work item"
    },
    {
        "op": "add",
        "path": "/fields/System.Description",
        "value": "This is a sample  work item created via API."
    }
]

# Make the request to create the work item
response = requests.post(url, json=data, headers={'Content-Type': 'application/json-patch+json'}, auth=('', personal_access_token))

if response.status_code == 200:
    work_item = response.json()
    print(f"Work item created: {work_item['id']}, title: {work_item['fields']['System.Title']}")
else:
    # Print the raw response content for debugging
    print(f"Failed to create work item: {response.status_code}")
    print(f"Response content: {response.text}")

  1. 使用 Azure DevOps Python 包 create_work_item 使用 python 脚本在 Azure 板上创建问题工作项。
from azure.devops.connection import Connection
from azure.devops.v7_1.work_item_tracking.models import JsonPatchOperation
from msrest.authentication import BasicAuthentication

# Replace with your Azure DevOps organization and project details
organization_url = 'https://dev.azure.com/orgname'
project = ''
personal_access_token = '' 
work_item_type = 'Issue'  # e.g., Task, Bug

# Create a connection to the Azure DevOps organization
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)

# Get the work item tracking client
wit_client = connection.clients.get_work_item_tracking_client()

#  Data for the new work item
document = [
    JsonPatchOperation(
        op='add',
        path='/fields/System.Title',
        value='Sample  work item'
    ),
    JsonPatchOperation(
        op='add',
        path='/fields/System.Description',
        value='This is a sample  work item created via API.'
    )
]

# Create the work item
work_item = wit_client.create_work_item(
    document=document,
    project=project,
    type=work_item_type
)

# Print the created work item details
print(f"Work item created: {work_item.id}, title: {work_item.fields['System.Title']}")

我看到了很多 YouTube 视频,其中展示了 PAT 的创建,但没有介绍如何在管道中使用它。

要在管道中使用上述Python脚本,可以按照以下步骤操作:

  1. 在管道中创建一个名为

    PAT
    的秘密变量。

    secret variable

  2. 在管道 YAML 中使用 PythonScript@0 任务。

    包含三个 python 脚本的 YAML 示例:

trigger: none

pool:
  vmImage: windows-latest

steps:
- bash: pip install requests
- task: PythonScript@0
  inputs:
    scriptSource: 'inline'
    script: |
      import requests
      
      # Replace with your Azure DevOps organization and project details
      organization = ''
      project = ''
      pat = '$(PAT)'  
      work_item_type = 'Issue'  # e.g., Task, Bug
      
      # API endpoint for creating a work item
      url = f'https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${work_item_type}?api-version=7.0'
      
      # Data for the new work item
      data = [
          {
              "op": "add",
              "path": "/fields/System.Title",
              "value": "Sample  work item"
          },
          {
              "op": "add",
              "path": "/fields/System.Description",
              "value": "This is a sample  work item created via API."
          }
      ]
      
      # Make the request to create the work item
      response = requests.post(url, json=data, headers={'Content-Type': 'application/json-patch+json'}, auth=('', pat))
      
      if response.status_code == 200:
          work_item = response.json()
          print(f"Work item created: {work_item['id']}, title: {work_item['fields']['System.Title']}")
      else:
          # Print the raw response content for debugging
          print(f"Failed to create work item: {response.status_code}")
          print(f"Response content: {response.text}")
- task: PythonScript@0
  inputs:
    scriptSource: 'inline'
    script: |
      import requests
      import base64
      
      # Replace with your Azure DevOps organization and project details
      organization = ''
      project = ''
      pat = '$(PAT)'  
      work_item_type = 'Issue'  # e.g., Task, Bug, 
      
      # Encode the PAT for the Authorization header
      encoded_pat = base64.b64encode(f':{pat}'.encode()).decode()
      # Define the headers
      headers = {
          'Content-Type': 'application/json-patch+json',
          'Authorization': f'Basic {encoded_pat}'
      }
      
      # API endpoint for creating a work item
      url = f'https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/${work_item_type}?api-version=7.1-preview.3'
      
      # Data for the new work item
      data = [
          {
              "op": "add",
              "path": "/fields/System.Title",
              "value": "Sample  work item"
          },
          {
              "op": "add",
              "path": "/fields/System.Description",
              "value": "This is a sample work item created via REST API."
          }
      ]
      
      # Make the request to create the work item
      response = requests.post(url, headers=headers, json=data)
      
      if response.status_code == 200:
          work_item = response.json()
          print(f"Work item created: {work_item['id']}, title: {work_item['fields']['System.Title']}")
      else:
          # Print the raw response content for debugging
          print(f"Failed to create work item: {response.status_code}")
          print(f"Response content: {response.text}")

- bash: pip install azure-devops
- task: PythonScript@0
  inputs:
    scriptSource: 'inline'
    script: |
      from azure.devops.connection import Connection
      from azure.devops.v7_1.work_item_tracking.models import JsonPatchOperation
      from msrest.authentication import BasicAuthentication
      
      # Replace with your Azure DevOps organization and project details
      organization_url = 'https://dev.azure.com/orgname'
      project = ''
      personal_access_token = '$(PAT)' 
      work_item_type = 'Issue'  # e.g., Task, Bug
      
      # Create a connection to the Azure DevOps organization
      credentials = BasicAuthentication('', personal_access_token)
      connection = Connection(base_url=organization_url, creds=credentials)
      
      # Get the work item tracking client
      wit_client = connection.clients.get_work_item_tracking_client()
      
      #  Data for the new work item
      document = [
          JsonPatchOperation(
              op='add',
              path='/fields/System.Title',
              value='Sample  work item'
          ),
          JsonPatchOperation(
              op='add',
              path='/fields/System.Description',
              value='This is a sample  work item created via API.'
          )
      ]
      
      # Create the work item
      work_item = wit_client.create_work_item(
          document=document,
          project=project,
          type=work_item_type
      )
      
      # Print the created work item details
      print(f"Work item created: {work_item.id}, title: {work_item.fields['System.Title']}")

测试结果: result

© www.soinside.com 2019 - 2024. All rights reserved.