如何使用承载令牌从Azure DevOps调用REST API

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

我正在尝试使用Azure DevOps任务以编程方式将LUIS预测资源分配给LUIS应用,如here所述。简而言之,这涉及

我能够手动执行这些步骤,但是如何从Azure DevOps执行此操作?我试图从无代理作业中使用“调用REST API”任务,但是看不到如何检索和使用Bearer令牌。请注意,承载令牌已过期。

感谢您的建议。

azure-devops luis
1个回答
0
投票

您可以在管道中添加powershell任务以通过azure devops执行此操作。

获取Azure资源管理器令牌:可以参考下面的Powershell脚本来获取令牌。检查here,以获取有关在何处获取客户端ID和客户端密码的更多信息。请注意,这里的[[resource是“ https://management.core.windows.net/

$client_id = "{client id}" $client_secret = "{client secret}" $uri= "https://login.microsoftonline.com/{tenant id}/oauth2/token" $Body = @{ 'resource'= "https://management.core.windows.net/" 'client_id' = $client_id 'grant_type' = 'client_credentials' 'client_secret' = $client_secret } $params = @{ ContentType = 'application/x-www-form-urlencoded' Headers = @{'accept'='application/json'} Body = $Body Method = 'Post' URI = $uri } $response = Invoke-RestMethod @params $token = $response.access_token
获得令牌后,您可以将其传递给LUIS rest api。下面的脚本仅作为示例。

$LuisBody = @{ "azureSubscriptionId"= "{subscription_id}" "resourceGroup"= "{resource_group_name}" "accountName"= "{account_name}" } $Luisparams = @{ Headers = @{ Authorization = ("Bearer {0}" -f $token) # pass the token which got from above script "Ocp-Apim-Subscription-Key" = "{subscription key}" ContentType = "application/json" } Body = $LuisBody Method = 'Post' URI = "https://{endpoint}/luis/api/v2.0/apps/{appId}/azureaccounts" } Invoke-RestMethod @Luisparams

[另外一个blog您可能会有所帮助。
© www.soinside.com 2019 - 2024. All rights reserved.