【问题标题】:Azure DevOps Personal Access TokenAzure DevOps 个人访问令牌
【发布时间】:2021-11-15 06:10:01
【问题描述】:

我有我的 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 库却没有?

【问题讨论】:

  • 我使用 PAT 作为密码,没有指定用户名。所以,auth 是:MYPATHERE 然后编码为base64;注意前导冒号以将 PAT 指定为密码。
  • 如果我执行base64.b64encode(personal_access_token.encode('utf-8')) 对我的 PAT 进行编码并删除用户名,我会收到一个错误,即用户 '' 无权访问资源。
  • 我认为完整的username:password 字符串需要进行base64 编码,但我不会单独对密码进行编码。只需将用户名设置为空字符串,然后再试一次?
  • 我的用户名现在是空的,我在 PAT 中添加了一个 :。执行 base64 并得到相同的错误。

标签: python azure-devops oauth-2.0


【解决方案1】:

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

  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 库获取工作项,您可以将以下代码添加到您的示例中,如果您想获得60814 一个,它将获取id68 的工作项,只需使用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

【讨论】:

    猜你喜欢
    • 2020-08-31
    • 2021-12-24
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 2020-06-17
    • 2020-02-23
    相关资源
    最近更新 更多