【问题标题】:MS translator request not authorizedMS 翻译请求未经授权
【发布时间】:2021-12-29 05:58:03
【问题描述】:

我有这个简单的代码来翻译:

    var apiurl = 'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0';
    var my_token = 'xxxxxx'; // my sekret key
        jQuery.ajax({
            url: apiurl,
            dataType: "json",
            jsonp: "oncomplete",
            crossDomain: true,
            context: this,
            data: {
                text: 'Test simple text',
                from: 'en',
                to: 'de'
            },
            beforeSend: function (xhr) {
                xhr.setRequestHeader('Ocp-Apim-Subscription-Key', my_token);
                xhr.setRequestHeader('Content-type', 'application/json; charset=UTF-8');
                xhr.setRequestHeader('X-ClientTraceId', uuid());
                xhr.setRequestHeader('Ocp-Apim-Subscription-Region', 'westeurope');
            },
            contentType: "application/json; charset=UTF-8",
            type: 'POST',
            success: function (msg) {
                console.log(msg);
            },
            complete: function (msg) {
            },
            error: function (e) {
                console.log(e);
            }
        });

但我总是收到错误 401000 请求未授权,因为凭据丢失或无效。

我使用了图片中的秘密 ID。

【问题讨论】:

    标签: javascript jquery azure azure-web-app-service


    【解决方案1】:

    通过以下调整,代码应该可以工作:

    • my_token 中使用“Azure 门户 > 翻译资源 > 密钥和终结点 > 密钥 1 或密钥 2”下的订阅密钥之一
    • 检查 Ocp-Apim-Subscription-Region 的区域(也是身份验证所必需的),使用“Azure 门户 > 翻译资源 > 密钥和端点 > 区域”下的术语
    • 插入 apiurl&from=en&to=de
    • 数据更改为:JSON.stringify([{ 'text': 'Test simple text' }])

    var apiurl = 'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=en&to=de';
    var my_token = 'xxxxxx'; // subscription key
    jQuery.ajax({
        url: apiurl,
        dataType: "json",
        jsonp: "oncomplete",
        crossDomain: true,
        context: this,
        data: JSON.stringify([{ 'text': 'Test simple text' }]),
        beforeSend: function (xhr) {
            xhr.setRequestHeader('Ocp-Apim-Subscription-Key', my_token);
            xhr.setRequestHeader('Content-type', 'application/json; charset=UTF-8');
            xhr.setRequestHeader('X-ClientTraceId', uuid());
            xhr.setRequestHeader('Ocp-Apim-Subscription-Region', 'westeurope');
        },
        contentType: "application/json; charset=UTF-8",
        type: 'POST',
        success: function (msg) {
            console.log(msg);
        },
        complete: function (msg) {
        },
        error: function (e) {
            console.log(e);
        }
    });
    
    function uuid() {
      return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
        (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
      );
    }

    【讨论】:

      【解决方案2】:

      您的屏幕截图显示了 Azure Active Directory 中的应用注册配置页面 - 这是完全不同的东西。你想要的是creating a Translator resource,然后从“Keys and Endpoints”菜单中选择一个键:

      【讨论】:

      • 注册任何服务都这么难。
      • 现在可以用了吗?
      • 看起来不错。现在我有: {"error":{"code":400000,"message":"其中一个请求输入无效。"}}
      • 如何运行这段 JavaScript 代码?在浏览器或后端(例如 Node.js?)。如果在浏览器中,您可能会遇到翻译 API 未返回正确的“Access-Control-*”标头的问题,因此您的浏览器会因 CORS 而阻止请求。
      猜你喜欢
      • 2017-11-21
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 2012-08-17
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多