gpt4 book ai didi

python - 使用 Python 请求时,Azure DevOps REST API 返回 HTTP 状态 203

转载 作者:行者123 更新时间:2023-12-02 23:13:59 26 4
gpt4 key购买 nike

I saw this question关于Python requests尝试向 Azure DevOps REST API 进行身份验证并接收 HTTP 状态 203,非权威信息。当查看文本响应时,它只是登录页面的 HTML,并没有真正让我登录。我使用了 Authorization: Basic <BASE64 PAT> listed on their REST API page ,但它似乎不起作用。这是我的代码示例:

"""Using Python and Requests to interact with Azure DevOps REST API
"""


import base64
import pprint as pp
import requests


with open('ado_pat.txt', 'r') as file:
PERSONAL_AUTHENTICATION_TOKEN = file.read().replace('\n', '')

PAT_BASE_64 = base64.b64encode(
b'{PERSONAL_AUTHENTICATION_TOKEN}').decode('ascii')
COLLECTION = 'collection_name'
ORGANIZATION_URL = f'https://dev.azure.com/{COLLECTION}'
RESOURCE_PATH = '/_apis/projects?api-version=5.1'
HEADERS = {
'Authorization': f'Basic {PAT_BASE_64}',
'Accept': 'application/json'
}

try:
ADO_RESPONSE = requests.get(
ORGANIZATION_URL + RESOURCE_PATH, headers=HEADERS)

pp.pprint(ADO_RESPONSE)
pp.pprint(ADO_RESPONSE.text)
ADO_RESPONSE.raise_for_status()
except requests.exceptions.HTTPError as err:
pp.pprint(err)

这是我得到的回复:

<Response [203]>
(''\r\n'

然后它会显示整个登录页面。我会使用microsoft/azure-devops-python-api但我并没有真正理解或看到我可以调用的方法,也没有真正理解它是如何工作的。

--编辑工作示例--

这个例子现在可以运行了。

"""Using Python and Requests to interact with Azure DevOps REST API
"""


import base64
import pprint as pp
import requests


with open('ado_pat.txt', 'r') as file:
PERSONAL_AUTHENTICATION_TOKEN = file.read().replace('\n', '')

USERNAME = ""
USER_PASS = USERNAME + ":" + PERSONAL_AUTHENTICATION_TOKEN
B64USERPASS = base64.b64encode(USER_PASS.encode()).decode()

COLLECTION = 'collection_name'
ORGANIZATION_URL = f'https://dev.azure.com/{COLLECTION}'
RESOURCE_PATH = '/_apis/projects?api-version=5.1'
HEADERS = {
'Authorization': 'Basic %s' % B64USERPASS
}

try:
ADO_RESPONSE = requests.get(
ORGANIZATION_URL + RESOURCE_PATH, headers=HEADERS)

pp.pprint(ADO_RESPONSE)
pp.pprint(ADO_RESPONSE.text)
ADO_RESPONSE.raise_for_status()
except requests.exceptions.HTTPError as err:
pp.pprint(err)

最佳答案

Using a comment's link ,我能够使上面的代码工作。这是最终结果工作代码。

"""Using Python and Requests to interact with Azure DevOps REST API
"""


import base64
import json
import pprint as pp
import requests


with open('ado_pat.txt', 'r') as file:
PERSONAL_AUTHENTICATION_TOKEN = file.read().replace('\n', '')

USERNAME = ""
USER_PASS = USERNAME + ":" + PERSONAL_AUTHENTICATION_TOKEN
B64USERPASS = base64.b64encode(USER_PASS.encode()).decode()

COLLECTION = 'collection_name'
ORGANIZATION_URL = f'https://dev.azure.com/{COLLECTION}'
RESOURCE_PATH = '/_apis/securitynamespaces?api-version=5.1'
HEADERS = {
'Authorization': 'Basic %s' % B64USERPASS
}

try:
ADO_RESPONSE = requests.get(
ORGANIZATION_URL + RESOURCE_PATH, headers=HEADERS)

pp.pprint(ADO_RESPONSE)
pp.pprint(ADO_RESPONSE.text)
ADO_RESPONSE.raise_for_status()
except requests.exceptions.HTTPError as err:
pp.pprint(err)


with open('output.json', 'w') as file:
file.write(json.dumps(ADO_RESPONSE.json(), indent=4))

API 文档似乎没有指定如何正确编码 PAT,需要重新编写。

关于python - 使用 Python 请求时,Azure DevOps REST API 返回 HTTP 状态 203,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58614515/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com