Pre*_*red 3 python azure python-requests azure-devops
我看到了有关Pythonrequests尝试向Azure DevOps REST API进行身份验证并接收HTTP状态203,非权威信息的问题。当查看文本响应时,它只是登录页面的 HTML,并没有真正让我登录。我使用了Authorization: Basic <BASE64 PAT> 他们的 REST 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', '')
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)
Run Code Online (Sandbox Code Playgroud)
这是我得到的回应:
<Response [203]>
(''\r\n'
Run Code Online (Sandbox Code Playgroud)
然后它显示整个登录页面。我会使用microsoft/azure-devops-python-api但我并没有真正理解或看到我可以调用的方法,也没有真正理解它是如何工作的。
--编辑工作示例--
这个例子现在可以运行了。
<Response [203]>
(''\r\n'
Run Code Online (Sandbox Code Playgroud)
使用评论的链接,我能够使上面的代码正常工作。这是最终结果工作代码。
"""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))
Run Code Online (Sandbox Code Playgroud)
API 文档似乎没有指定如何正确编码 PAT,需要重新设计。
| 归档时间: |
|
| 查看次数: |
5276 次 |
| 最近记录: |