簡體   English   中英

使用 Python 獲取 Azure DevOps 項目工作項時的身份驗證

[英]Authentication when Get Azure DevOps Project Work Items using Python

我正在嘗試在 Azure DevOps 中獲取項目的所有工作項。

我面臨的問題是身份驗證。 在發送此帖子請求之前如何進行身份驗證/登錄?

文檔將安全性顯示為0auth2 但是,我有一個個人訪問令牌......正確的方法是什么? 如何發送經過身份驗證的 POST 請求。 我是否必須注冊應用程序並獲得0auth令牌等。或者 PAT 就足夠了?

import requests
import base64

pat = 'nbq'
authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')), 'ascii')

headers = {
    'Accept': 'application/json',
    'Authorization': 'Basic '+authorization
}
# response = requests.get(url="https://dev.azure.com/company/_apis/projects?api-version=5.1", headers=headers).json()

data = {
  "query": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Task'"
}
wo_it = requests.post('https://dev.azure.com/company/example_project/_apis/wit/wiql?api-version=5.1',data=data)
print(wo_it.text)

Azure DevOps REST API supports several different auth ways, eg OAuth 2.0, Azure AD auth, PAT.

對於您正在使用的具體API _apis/wit/wiql ,使用PAT就足夠了,您不需要注冊應用程序。 And you should know OAuth 2.0 and Azure AD auth are both user-interactive way, if you want to call the API in python with non-interactive way, PAT is the option.

PAT 使用基本身份驗證,每次發送 API 請求時,您都需要對其進行 base64 編碼並將編碼后的內容傳遞給請求正文。 我看到你在列出項目時只是使用令牌,但在調用_apis/wit/wiql時沒有使用它,你也需要這樣做,然后它會起作用。

參考 - https://docs.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/authentication-guidance?view=azure-devops

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM