【发布时间】: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