![](/img/trans.png)
[英]Python InvalidHeader error when trying to send http requests to Azure REST API
[英]Azure DevOps REST API returning HTTP Status 203 when using Python Requests
I saw this question about Python requests
attempting to authenticate to Azure DevOps REST API and receiving HTTP Status 203, Non-Authoritative Information. 查看文本響應時,它只是登錄頁面的 HTML 並沒有真正讓我登錄。我使用了Authorization: Basic <BASE64 PAT>
在他們的 REST ZDB974238714CA8DE634A7CE1D083A14 頁面上列出,但它似乎不起作用。 這是我的代碼示例:
"""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 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 並且需要返工。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.