Azure DevOps 个人访问令牌

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

我有我的 PAT 和一些示例代码来在我的组织内拉取项目。然而,当我尝试使用

GET
命令来读取工作项时,我得到
response 203
。我的大部分谷歌搜索都得出了三种解决方案,但对我来说都没有成功。有人提到使用 base64 和
:
编辑我的 PAT。第二个说使用 oauth2 python 库。第三种解决方案是下面的代码。

PE_DEVOPS_NAME = "DevOpsArea"

# Fill in with your personal access token and org URL
personal_access_token = ':XXXX'
organization_url = 'https://dev.azure.com/organization/'

post_url = organization_url + PE_DEVOPS_NAME + "/_apis/wit/workitems/$task?api-version=6.0"
get_url = organization_url + PE_DEVOPS_NAME + "/_apis/wit/workitems/60814?api-version=6.0"

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

# Get a client (the "core" client provides access to projects, teams, etc)
core_client = connection.clients.get_core_client()

# Get the first page of projects
get_projects_response = core_client.get_projects()

for project in get_projects_response.value:
    if project.name == PE_DEVOPS_NAME:
        print(project.name)

response = requests.get(get_url)
print(get_url)
print(response.json)

我在上面的代码中是否遗漏了一些东西,导致我的 PAT 无法工作?

我继续寻找,发现了以下代码:

username = ''
personal_access_token = 'XXXX'
login_info = username + ":" + personal_access_token
b64 = base64.b64encode(login_info.encode()).decode()
headers = {"Authorization" : "Basic %s" % b64}


organization_url = 'https://dev.azure.com/organization/'

post_url = organization_url + PE_DEVOPS_NAME + "/_apis/wit/workitems/$task?api-version=6.0"
get_url = organization_url + PE_DEVOPS_NAME + "/_apis/wit/workitems/60814?api-version=6.0"

response = requests.get(get_url, headers=headers)
print(response.json())

此代码适用于我的

GET
命令。我将用
POST
进行测试。任何人都可以解释为什么这有效但 azure python 库不起作用?

python azure-devops oauth-2.0
2个回答
0
投票

对于代码中的第一个示例,它肯定不会工作,代码中有两个错误。

  1. 当您使用

    response = requests.get(get_url)
    时,它不使用任何凭证来调用REST API。

  2. 即使你使用

    Azure DevOps Python API
    python 库,它也应该是
    personal_access_token = 'XXXX'
    而不是
    personal_access_token = ':XXXX'
    ,你不应该包含
    :
    字符。

  3. 对于

    203
    状态码,如果您在最后一行使用
    print(response.text)
    代替
    print(response.json)
    ,您可以轻松发现它只是返回登录页面。

要使用

Azure DevOps Python API
python 库获取工作项,您可以将以下代码添加到您的示例中,它会获取
id
68
的工作项,如果您想获取
60814
一个,只需使用
range(60814, 60815)

wit_client = connection.clients.get_work_item_tracking_client()
desired_ids = range(68, 69)
print(desired_ids)
work_items = wit_client.get_work_items(ids=desired_ids)
for wit in work_items:
    print(wit)

参考 - https://github.com/microsoft/azure-devops-python-samples/blob/552544e9cde70269e37784aff2e62dd97420b862/src/samples/work_item_tracking.py


0
投票

我能够使用 Microsoft python 客户端 https://github.com/microsoft/azure-devops-python-api(版本 7.1.0b4)并使用 requests 包连接到 ADO API。请参阅下面对我有用的代码。当然,您需要添加一些检查以使其防弹。

import requests
import base64
import json
from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication

personal_access_token = 'YOUR_ACCESS_TOKEN'
organization = 'YOUR_ORGANIZATION'
organization_url = f'https://dev.azure.com/{organization}'

#method 1    
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
core_client = connection.clients.get_core_client()
get_projects_response = core_client.get_projects()
for project in get_projects_response:
    print(project.id, project.name)

#method 2
authorization = str(
    base64.b64encode(bytes(":" + personal_access_token, "ascii")), "ascii"
)

projects_url = f"https://dev.azure.com/{organization}/_apis/projects?api-version=7.1-preview.4"
headers = {"Accept": "application/json", "Authorization": "Basic " + authorization}

response = requests.get(url=projects_url, headers=headers)
response_dict = json.loads(response.text)
print(response_dict['value'][0]['id'], response_dict['value'][0]['name'])
© www.soinside.com 2019 - 2024. All rights reserved.