【问题标题】:How do I Invoke a REST API from Azure DevOps using Bearer Token如何使用承载令牌从 Azure DevOps 调用 REST API
【发布时间】:2019-10-28 07:46:40
【问题描述】:

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

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

感谢您的建议。

【问题讨论】:

  • Invoke Rest API 任务中的token是怎么给的?
  • 我在 DevOps 中创建了一个没有用户名/密码的通用服务连接,并将其分配给 Invoke REST API 任务。
  • 我尝试将标头中的令牌硬编码为 {"Content-Type":"application/json", "Authorization":"Bearer "...},但这给了我“(500)内部服务器错误”。
  • 但是即使这个硬编码的令牌可以工作,获取这个令牌并将其传递给 POST 调用的正确方法是什么?

标签: azure-devops azure-language-understanding


【解决方案1】:

您可以在管道中添加 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 可能对您有帮助。

更新: 使用以下脚本通过 Azure CLI 获取 Azure 资源管理器令牌:

az account get-access-token --resource=https://management.core.windows.net/ | jq -r .accessToken

查看官方文档herehere为例。

【讨论】:

  • 很好的解决方案!我从 Azure 门户的应用注册中获取了 client_id,并为 client_secret 生成了一个密钥。唯一的缺点是我必须管理一个额外的客户端密码,我想知道这是否可以更简单?我的应用程序/服务主体已在 DevOps 中注册为“ARM 服务连接”。如果我使用“Azure CLI”powershell 任务,我可以使用此服务连接。那么是否有可能通过 Azure AD 获取令牌(因此 aviod clien_secret)?
  • 你可以试试这个 Azure CLI az account get-access-token --resource "https://management.core.windows.net/" | jq -r .accessToken 。我更新了上面的答案。
  • 我最终完成了一个 Azure Powershell 任务,具有类似的令牌检索:$context = Get-AzContext $azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile; $profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azureRmProfile); $token = $profileClient.AcquireAccessToken($context.Subscription.TenantId).AccessToken;,然后是 `Invoke-RestMethod。
猜你喜欢
  • 2021-01-31
  • 1970-01-01
  • 2020-04-18
  • 2021-03-10
  • 1970-01-01
  • 2020-06-16
  • 2021-03-10
  • 2020-11-26
  • 1970-01-01
相关资源
最近更新 更多