【问题标题】:Azure Translator API always gives me 404Azure Translator API 总是给我 404
【发布时间】:2020-06-19 07:42:21
【问题描述】:

我正在尝试通过免费试用订阅来使用翻译 API。创建后,我设置资源组并向其添加认知服务。在那个认知服务页面上,我按照“快速入门”指南获得了一对密钥和 ENDPOINT url。

然后我跟着这个文件。它说使用转换器 API 需要 API 密钥和端点。我从 RESOURCE MANAGEMENT>Keys and Endpoint 部分获取它们。

https://docs.microsoft.com/en-gb/azure/cognitive-services/translator/quickstart-translate?pivots=programming-language-javascript

但是每次我发送请求时,我总是从这个 API 获得 404 状态码。有什么我做错了吗?如何使用此 API?

订阅 ID:zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

我的代码:

 const subscriptionKey = 'my-sub-key'; // There are two keys, key1 and key2. I use key1 here.
 const endpoint = 'https://japaneast.api.cognitive.microsoft.com/';
 const options = {
        method: 'POST',
        baseUrl: endpoint,
        url: 'translate',
        qs: {
          'api-version': '3.0',
          'to': 'en'
        },
        headers: {
          'Ocp-Apim-Subscription-Key': subscriptionKey,
          'Content-type': 'application/json',
          'X-ClientTraceId': uuidv4().toString()
        },
        body: [{
          'text': 'hello world'
        }],
        json: true,
 };
 request(options, function (err, resFromMicrosoft, body) {
        res.json(resFromMicrosoft);
}

错误响应:

{
        "statusCode": 404,
        "body": {
            "error": {
                "code": "404",
                "message": "Resource not found"
            }
        },
        "headers": {
            "content-length": "56",
            "content-type": "application/json",
            "apim-request-id": "e2ae69cc-b93c-4db2-aef4-47096eb3ec61",
            "strict-transport-security": "max-age=31536000; includeSubDomains; preload",
            "x-content-type-options": "nosniff",
            "date": "Fri, 19 Jun 2020 06:11:24 GMT",
            "connection": "close"
        },
        "request": {
            "uri": {
                "protocol": "https:",
                "slashes": true,
                "auth": null,
                "host": "japaneast.api.cognitive.microsoft.com",
                "port": null,
                "hostname": "japaneast.api.cognitive.microsoft.com",
                "hash": null,
                "search": "?api-version=3.0&to=en",
                "query": "api-version=3.0&to=en",
                "pathname": "/translate",
                "path": "/translate?api-version=3.0&to=en",
                "href": "https://japaneast.api.cognitive.microsoft.com/translate?api-version=3.0&to=en"
            },
            "method": "POST",
            "headers": {
                "Ocp-Apim-Subscription-Key": "my-sub-key",
                "Content-type": "application/json",
                "X-ClientTraceId": "8eedf6f4-db0c-45cb-a95b-92a2797df067",
                "accept": "application/json",
                "content-length": 83
            }
        }
    }

【问题讨论】:

    标签: azure microsoft-translator


    【解决方案1】:

    如文档中所述,请使用全局端点 - https://api.cognitive.microsofttranslator.com/

    【讨论】:

    • 我也已经尝试过了。但这次只得到了 401。
    【解决方案2】:

    我看到您关于使用全局端点后获得 401 的评论。 您仍然需要使用 Swetha 提到的全局端点。

    这可能有助于https://github.com/MicrosoftDocs/azure-docs/issues/57430#event-3480744006

    (来自上一页) “看起来您正在使用一个通用的认知服务资源和一个密钥。大多数服务已经转移到使用单独的资源类型,如果您可以创建和使用翻译资源,文档中提到的步骤应该可以按原样工作。”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-08
      • 2017-11-24
      • 2020-12-12
      • 2021-07-18
      • 2022-01-05
      • 2018-07-25
      • 1970-01-01
      • 2018-12-13
      相关资源
      最近更新 更多