通过 Python 使用 PAT 令牌更新 Azure Devops 中的存储库

Updating repositories in Azure Devops with PAT token through Python

我正在尝试通过 API 禁用 Azure Devops 中的存储库,但我在向我的 Azure devops 组织进行身份验证时遇到了问题。我的请求是正确的,但我一直收到

401 client error Unauthorized for URL

我有一个 PAT 令牌来进行身份验证,我只是不确定如何将其添加到请求正文中以便能够对 Azure Devops 进行身份验证。下面是我用来完成此操作的代码。我查看了所有文档,但找不到完成此操作的语法,但我知道可以使用 PAT 令牌进行身份验证。

print("Would you like to Disable this repository in Azure Devops?")
disable = input("please select y/n: ")
if disable == 'y':
    pat = 'example'
    authorization = str(base64.b64encode(bytes(':' + pat, 'ascii')), 'ascii')
    headers = {
        'Authorization': 'basic '+authorization
    }
    organization = "exmaple"
    disable_payload = '{"isDisabled": true }'
    disable_url = requests.patch(f"https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{i}?api-version=7.1-preview.1",
                                 data=disable_payload)
    disable_url.raise_for_status()
    print("Repo has been disabled")

对于任何想知道的人,我能够解决这个问题。下面是最终的代码输出。负载中的数据必须更改为 json。必须添加 header,但必须是 header。最后,由于某种原因,我的 i 变量在其末尾附加了一个“\n”,这导致 URL 出错。我从变量中剥离了它并且它起作用了!

                       # print("Would you like to Disable this repository in Azure Devops?")
                 disable = input("please select y/n: ")
                 if disable == 'y':
                     pat = 'example'
                     authorization = str(base64.b64encode(bytes(':' + pat, 'ascii')), 'ascii')
                     headers = {
                         'Authorization': 'basic '+authorization
                     }
                     organization = "example"
                     disable_payload = {"isDisabled": True }
                
                     i = i.strip("\n")
                     disable_url = requests.patch(f"https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{i}?api-version=7.1-preview.1",
                                                  json=disable_payload, headers=headers)
                     disable_url.raise_for_status()
                     print("Repo has been disabled")