Flo*_*wab 3 python api azure azure-devops
我正在尝试检查 PAT 令牌何时到期,以便我可以在特定令牌即将到期时创建警报/通知,并在到期之前替换它。
有一个 API 可查询组织内的所有 PAT: https://learn.microsoft.com/en-us/rest/api/azure/devops/tokenadmin/personal%20access%20tokens/list ?view=azure-devops-休息-5.1
遗憾的是,这个 API 需要组织本身的写入权限,而我没有。使用下面的脚本我收到以下错误:
azure.devops.exceptions.AzureDevOpsServiceError: Access Denied: XXX needs the following permission(s) to perform this action: Edit instance-level information
这让我想到了我的问题:有没有一种方法/API 可以在没有组织本身权限的情况下查询我的个人 PAT?
这里是Python脚本的当前草案,用于检查PAT以供参考:
#!/usr/bin/env python
from msrest.authentication import BasicAuthentication
from azure.devops.connection import Connection
# Fill in with your personal access token and org URL
personal_access_token = 'XXX'
organization_url = 'https://dev.azure.com/XXX'
# Create a connection to the org
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)
# Get personal subject_descriptor by mail
graph_client = connection.clients_v6_0.get_graph_client()
user_descriptor = None
continuation_token = None
while True:
graph_response = graph_client.list_users(continuation_token=continuation_token)
continuation_token = graph_response.continuation_token
for u in graph_response.graph_users:
if u.mail_address == "my@mail":
user_descriptor = u.descriptor
break
if continuation_token == None:
break
# Get a client for token admin
token_admin_client = connection.clients_v6_0.get_token_admin_client()
# Get list of personal access tokens
tokens_response = token_admin_client.list_personal_access_tokens(user_descriptor)
print(tokens_response)
Run Code Online (Sandbox Code Playgroud)
有没有一种方法/API 可以在没有组织本身权限的情况下查询我的个人 PAT?
根据我的测试,这个API确实存在。官方文档中没有这个API。
我们可以在浏览器控制台 -> 网络选项卡中获取它。
这是模板:
https://vssps.dev.azure.com/Org name/_apis/Token/SessionTokens?displayFilterOption=1&createdByOption=3&sortByOption=3&isSortAscending=true&startRowNumber=1&pageSize=100&api-version=5.0-preview.1
Run Code Online (Sandbox Code Playgroud)
这个Rest API不需要有权限Edit instance-level information。
您只需将“令牌管理”范围授予个人访问令牌即可。然后就可以成功运行API了。
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
4255 次 |
| 最近记录: |