无法使 Microsoft Translator API 字典查找工作

Can't get the Microsoft Translator API Dictionary Lookup to work

我订阅了免费的 Azure 试用版以使用字典查找功能并获得了这个端点和(已编辑)API 密钥:

Grab your keys and endpoint

key1

084***13

Endpoint

https://westeurope.api.cognitive.microsoft.com/

Every call to Cognitive Services requires the subscription key above. This key needs to be either passed through a query string parameter or specified in the request header. To manage your keys, use the Keys option from the left menu

然后在 this page 我找到了一个简单的例子,似乎我只需要替换密钥:

curl -X POST "https://api.cognitive.microsofttranslator.com/dictionary/lookup?api-version=3.0&from=en&to=es" -H "Ocp-Apim-Subscription-Key: <client-secret>" -H "Content-Type: application/json" -d "[{'Text':'fly'}]"

我在我的命令行中测试它,替换密钥,但没有成功:

curl -X POST "https://api.cognitive.microsofttranslator.com/dictionary/lookup?api-version=3.0&from=en&to=es" -H "Ocp-Apim-Subscription-Key: 084***13" -H "Content-Type: application/json" -d "[{'Text':'fly'}]

{"error":{"code":401000,"message":"The request is not authorized because credentials are missing or invalid."}}

我尝试更改端点,但也没有成功:

curl -X POST "https://westeurope.api.cognitive.microsoft.com/dictionary/lookup?api-version=3.0&from=en&to=es" -H "Ocp-Apim-Subscription-Key: 084***13" -H "Content-Type: application/json" -d "[{'Text':'fly'}]

{"error":{"code":"404","message": "Resource not found"}}

我想我遗漏了一些明显的东西,但该文档并不完全适合新手。我该怎么办?

您需要添加地区!

这取决于您订阅的资源,因为有 2 个选项,Translator Text API and Cognitive Services multi-service API

我可以看到你选择了 Cognitive Services multi-service 所以,在这种情况下你需要添加 documentation

中规定的 Ocp-Apim-Subscription-Region

When you use a multi-service secret key, you must include two authentication headers with your request. The first passes the secret key, the second specifies the region associated with your subscription.

  • Ocp-Apim-Subscription-Key
  • Ocp-Apim-Subscription-Region

我在下面添加了,你只需要把<your-key><your-region>换成你自己的就行了!

curl -X POST "https://api.cognitive.microsofttranslator.com/dictionary/lookup?api-version=3.0&from=en&to=es" -H "Ocp-Apim-Subscription-Key: <your-key>" -H "Ocp-Apim-Subscription-Region: <your-region>" -H "Content-Type: application/json" -d "[{'Text':'fly'}]"