Azure DevOps REST API创建个人访问令牌(PAT) - PattokenError:UserIdquired

发布于 2025-02-02 16:25:31 字数 2454 浏览 4 评论 0原文

当我使用get动词时,我可以列出现有的拍拍。

当我使用帖子动词创建PAT时,我会得到200个确定的响应。

有效负载显示:

{“ 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/Managendidentity,因为Microsoft不支持DevOps REST API的这些型号,并且并非所有DevOps REST API功能都可以在Azure CLI中使用。

https://learn.microsoft.com/en-us/azure/devops/integrate/get-started/get-started/authentication/authentication/authentication-guidance?view = azure-devops&amp.amp; 2f%2F

我现在有一个有效的携带者令牌

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;

这个响应看起来很棒,而且我知道携带者令牌正在工作。由于这是通过Azure CLI创建的新创建的广告用户,因此我希望我在响应中确实会收到一个空的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

:UserIdquired

I am able to list existing PATs when I use the GET verb.

I get a 200 OK response when I create a PAT using the POST verb.

The payload shows:

{"patToken":null,"patTokenError":"userIdRequired"}

This error is listed in the documentation but does not explain how to resolve it.

var clientBearer = new RestClient(@"https://login.microsoftonline.com");
var requestBearer = new RestRequest(
quot;{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;

Password Authentication was selected as the only viable non-interactive authentication method. This user is replacing a ServicePrincipal/ManagedIdentity because Microsoft does not support those models for Devops REST API and not all Devops REST API functionality is available in the Azure CLI.

https://learn.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/authentication-guidance?view=azure-devops&viewFallbackFrom=azure-devops%2F%2F

I now have a valid bearer Token

var devopsClient = new RestClient(@"https://vssps.dev.azure.com");
var patRequest = new RestRequest(
quot;{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;

This response looks great and I know that the bearer token is working. As this is a newly created AD user created through the Azure CLI I am expecting an empty array of PAT which I do receive in the response.

So far, So good

//create a token
var body = new
{
    displayName = "targetName",
    scope = "app_token",
    validTo = validTo,
    allOrgs = true
};
patRequest.AddJsonBody(body);
var patCreateResponse = devopsClient.ExecutePostAsync(patRequest).Result;

This final response is the problem and it contains the

patTokenError : userIdRequired

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生活了然无味 2025-02-09 16:25:31

从userIdRequired的错误中,似乎没有从访问令牌提供用户ID或不允许用户。带有“> Access-azure-devops-rest-api-with-oauth ”。

如果这是问题 验证该第三方应用程序通过
Oauth尚未被您的组织的管理员禁用

https://dev.azure.com/ name}/_设置/组织Policy

作为另一项工作
创建像这样的pat 所以参考

我自己还是自己检查一下,但是如果为应用程序提供了适当的示波器,请在Azure AD注册中检查,并访问REST API并授予同意。 DevOps可能会导致用户无法通过REST API访问DevOps。

  • 请注意, 不支持用一个
    Azure DevOps服务中的服务帐户。

根据 doken lifecycle management rest rest rest API用于Azure Devops Services-Azure Devops Services Services Services Services Services Services Services REST API REST API |微软文档

令牌的范围应为499B84AC-1321-427F-AA17-267CA67CA6975798/.DEFAULT哪个提供访问Azure DevOps Service Rest REST API >。一旦拥有令牌,请将其用作
您请求的授权标题的携带者令牌。


看起来您已经正确提供了范围,还请确保已授予管理员同意。
另外,请确保提供配置文件用户。在Azure AD中阅读范围,以检查登录的用户配置文件,因为此API仅适用于属于Azure AD租户的用户,并具有有效的Azure订阅。

同样在其他错误的情况下,建议撤销PAT。

使用个人访问令牌

注意:

参考:

From the error UserIdRequired, it seems UserId is not provided from the access token or the user is not allowed .See 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

I dint check it myself,but please check , in the azure AD registration, if proper scopes are provided for the application and to access REST API and granted consent.User gets access token for user ,but due to lack of proper permissions to access Azure devops may lead to user not being able to access devops through rest api.

  • Please note that it doesn’t support to create a PAT token with a
    service account in Azure DevOps Service.

According to Token Lifecycle Management REST API for Azure DevOps Services - Azure DevOps Services REST API | Microsoft Docs

The scope for the token should be 499b84ac-1321-427f-aa17-267ca6975798/.default which provides access to Azure DevOps Services REST API . Once you have the token, use it as
a Bearer token in Authorization header of your request.

It looks like you have provided scope correctly , also make sure it is granted admin consent.
Also make sure to provide profile User.Read scopes in azure ad , to check logged in users profile as this API is only available to users that are part of an Azure AD tenant with an active Azure subscription.

Also for safe side in other cases of PatToken in error, it is recommended to revoke the PAT.

Referred from Use personal access tokens

NOTE:
enter image description here

References:

  1. Authorize access to REST APIs with OAuth 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文