我想使用 Azure DevOps 构建管道中的 Azure AI 文本翻译,而无需使用任何密钥进行身份验证。 我之前曾使用工作负载联合身份验证从管道进行顺利身份验证,因此如果 Azure AI Translator 也可以实现这一点,那就太好了。
我已在 Azure 门户中设置了一个资源组,其中包含“翻译者”类型的资源。 我还在我的 Azure DevOps 项目中设置了一个引用该资源组的(Azure 资源管理器)服务连接,并且我可以看到在 Microsoft Entra 中为我创建了联合凭据。
现在,下一步通常是在 Microsoft Entra 中设置联合凭据的权限。对于我之前的联合凭证,我使用了“请求 API 权限”。但在这种情况下选择什么并不明显。 我可以使用一些有关如何设置权限以便能够从我的管道使用文本翻译的指导。
DevOps 管道用于 CICD 流程。根据下面翻译器的其余 api 文档,您应该使用
KEY
、Region
和 Web Api
而不是工作负载身份联合来实现自动化。
yaml 示例:
pool:
vmImage: Windows-latest
steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
# Define the paramters to translate a string
$subscriptionKey = ${env:KEY}
$region = "eastus"
$text = "Hello, world!"
$toLanguage = "fr"
# Define the function to call the Translator Text API
function Translate-Text ($subscriptionKey, $region, $text, $toLanguage) {
$uri = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=$toLanguage"
$headers = @{
"Ocp-Apim-Subscription-Key" = $subscriptionKey
"Ocp-Apim-Subscription-Region" = $region
"Content-Type" = "application/json"
}
$body = ConvertTo-Json @(@{ Text = $text })
$response = Invoke-RestMethod -Uri $uri -Method Post -Body $body -Headers $headers
return $response[0].translations[0].text
}
$translatedText = Translate-Text -subscriptionKey $subscriptionKey -region $region -text $text -toLanguage $toLanguage
Write-Output "Translated text: $translatedText"
env:
KEY: $(key)
输出,被翻译成 fr: