为 pat Rest API 生成 Bearer Token

问题描述 投票:0回答:1

我正在尝试使用 Azure Devops Rest API 生成 pat 令牌。我需要在标头下的身份验证中传递不记名令牌。我已按照此文档生成不记名令牌,但它不起作用。如何正确生成token并使用它。

azure-devops azure-active-directory
1个回答
0
投票

如何正确生成token并使用它。

您可以参考以下步骤生成token来创建PAT。

步骤1:在Microsoft Entra ID -> 应用程序注册

中创建AAD应用程序

第2步:在AAD应用程序中添加委派的Azure DevOps user_impersonation权限。

例如:

enter image description here

Step3:在 Authentication 选项卡中添加 https://jwt.ms 作为重定向 URI。

enter image description here

Step4:将 AAD 服务主体添加到组织中。请参阅步骤:在 Azure DevOps 组织中添加和管理服务主体

第五步:运行以下请求以获取代码:

https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/authorize
?client_id=<appID>
&response_type=code
&redirect_uri=https://jwt.ms
&response_mode=query
&scope= 499b84ac-1321-427f-aa17-267ca6975798/.default
&state=12345

您可以在响应中记录代码值:

enter image description here

第6步:您可以使用以下PowerShell示例来获取aad令牌。

$tenantID=""
$clientId=""
$clientSecret=""
$redirectUri="https://jwt.ms"
$code=""   # code from above step


# Azure token endpoint
$tokenUrl = "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token"

# Prepare the body of the request
$body = @{
    grant_type    = "authorization_code"
    client_id     = $clientId
    client_secret = $clientSecret
    scope         = "499b84ac-1321-427f-aa17-267ca6975798/.default openid profile offline_access"
    code          = $code
    redirect_uri  = $redirectUri
}

# Prepare the headers
$headers = @{
    "Content-Type" = "application/x-www-form-urlencoded"
}

# Make the POST request
$response2 = Invoke-RestMethod -Method Post -Uri $tokenUrl -Headers $headers -Body $body
echo $response2.refresh_token

Step7:您可以使用aad令牌生成Azure DevOps PAT。

© www.soinside.com 2019 - 2024. All rights reserved.