【问题标题】:How to use azure translator with authorization token?如何使用带有授权令牌的 azure 翻译器?
【发布时间】:2021-07-03 10:19:03
【问题描述】:

我想使用带有授权令牌的 azure 翻译服务。有很多资源解释如何使用订阅密钥进行操作,但我找不到任何解释授权令牌使用的资源。我可以获得授权令牌,但是当我使用它发送请求时,响应状态码是 401。 这就是我发送请求的方式:

curl POST 'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=es' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer token' \
--data-raw '[{'\''Text'\'':'\''Hello World!'\''}]'

【问题讨论】:

标签: azure microsoft-cognitive azure-cognitive-services microsoft-translator


【解决方案1】:

如果您想使用认证令牌调用文本翻译API,请参考以下步骤

  1. 获取令牌。如果您的服务是全局服务,则端点为 https://api.cognitive.microsoft.com/sts/v1.0/issueToken
curl -v -X POST \
"https://YOUR-REGION.api.cognitive.microsoft.com/sts/v1.0/issueToken" \
-H "Content-type: application/x-www-form-urlencoded" \
-H "Content-length: 0" \
-H "Ocp-Apim-Subscription-Key: YOUR_SUBSCRIPTION_KEY"

此外,请注意身份验证令牌的有效期为 10 分钟。多次调用 Translator 时应重复使用该令牌。但是,如果您的程序在很长一段时间内向翻译器发出请求,那么您的程序必须定期(例如,每 8 分钟)请求一个新的访问令牌。

  1. 调用 API
curl -X POST 'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=en&to=de' \
-H 'Authorization: Bearer YOUR_AUTH_TOKEN' \
-H 'Content-Type: application/json' \
--data-raw '[{ "text": "How much for the cup of coffee?" }]' | json_pp

更多详情请参考herehere

【讨论】:

  • 非常感谢
猜你喜欢
  • 2018-07-18
  • 1970-01-01
  • 2018-11-21
  • 2021-04-15
  • 1970-01-01
  • 2022-10-23
  • 1970-01-01
  • 2023-01-19
  • 1970-01-01
相关资源
最近更新 更多