当我使用 GET 动词时,我能够列出现有的 PAT。
当我使用 POST 动词创建 PAT 时,我收到 200 OK 响应。
有效负载显示:
{"patToken":null,"patTokenError":"userIdRequired"}
文档中列出了此错误,但没有解释如何解决它。
var clientBearer = new RestClient(@"https://login.microsoftonline.com");
var requestBearer = new RestRequest($"{tenantID}/oauth2/v2.0/token");
requestBearer.AddHeader("Content-Type", "application/x-www-form-urlencoded");
requestBearer.AddParameter("client_id", appRegistration.appId);
requestBearer.AddParameter("client_secret", appRegistrationSecret);
requestBearer.AddParameter("scope", "499b84ac-1321-427f-aa17-267ca6975798/.default");
requestBearer.AddParameter("username", username);
requestBearer.AddParameter("password", password);
requestBearer.AddParameter("grant_type", "password");
var responseBearer = clientBearer.ExecutePostAsync(requestBearer).Result;
var azureBearerToken = JsonConvert.DeserializeObject<AzureBearerToken>(responseBearer.Content);
var devopsBearerToken = azureBearerToken.token_type + " " + azureBearerToken.access_token;
密码身份验证被选为唯一可行的非交互式身份验证方法。该用户正在替换 ServicePrincipal/ManagedIdentity,因为 Microsoft 不支持 Devops REST API 的这些模型,并且并非所有 Devops REST API 功能都在 Azure CLI 中可用。
我现在有一个有效的不记名 token
var devopsClient = new RestClient(@"https://vssps.dev.azure.com");
var patRequest = new RestRequest($"{devopsOrganization}/_apis/tokens/pats?api-version=7.1-preview.1");
patRequest.AddHeader("Authorization", devopsBearerToken);
patRequest.AddHeader("Content-Type", "application/json");
var patListResponse = devopsClient.ExecuteGetAsync(patRequest).Result;
这个响应看起来很棒,我知道不记名 token 正在工作。由于这是通过 Azure CLI 创建的新创建的 AD 用户,因此我期望在响应中收到一个空的 PAT 数组。
到目前为止,一切顺利
//create a token
var body = new
{
displayName = "targetName",
scope = "app_token",
validTo = validTo,
allOrgs = true
};
patRequest.AddJsonBody(body);
var patCreateResponse = devopsClient.ExecutePostAsync(patRequest).Result;
最后的响应就是问题所在,它包含
patTokenError:userIdRequired
最佳答案
从错误 UserIdRequired 来看,似乎访问 token 未提供 UserId 或不允许该用户。请参阅 access-azure-devops-rest-api-with-oauth .
If that is the issue Verify that Third-party application access via OAuth hasn't been disabled by your organization's admin at https://dev.azure.com/{your-org-name}/_settings/organizationPolicy.
As another work around try to keep UserId in the Post request body while creating PAT something like this SO ref
我自己没有检查它,但请检查在azure AD注册中,是否为应用程序提供了正确的范围并访问REST API并授予同意。用户获取用户的访问 token ,但由于缺乏访问Azure devops的适当权限,可能会导致用户无法通过rest api访问devops。
- 请注意它不支持创建带有 Azure DevOps 服务中的服务帐户。
The scope for the token should be
499b84ac-1321-427f-aa17-267ca6975798/.default
whichprovides access to Azure DevOps Services REST API
. Once you have the token, use it as a Bearer token in Authorization header of your request.
看来您已经正确提供了范围,还要确保它已获得管理员同意。 另请确保在 azure ad 中提供配置文件 User.Read 范围,以检查登录的用户配置文件,因为此 API 仅适用于属于具有有效 Azure 订阅的 Azure AD 租户的用户。
另外,为了安全起见,在其他PatToken错误的情况下,建议撤销PAT。
引用自 Use personal access tokens
引用文献:
关于Azure Devops Rest API 创建个人访问 token (PAT) -- patTokenError : userIdRequired,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72427804/