如何使用API翻译SAP Leonardo?

How to use API translate of SAP Leonardo?

我需要有关 API SAP Leonardo 翻译的帮助。我构建了一个用于学习的翻译应用程序,并按照文档创建了方法 translate:

translate: function () {
            //Create JSON Model with URL
            var oModel = new sap.ui.model.json.JSONModel();

            var langTo = this.getView().byId("idTo").getSelectedKey();
            var langFrom = this.getView().byId("idFrom").getSelectedKey();

            var textOld = this.getView().byId("idOldText").getValue();

            //API Key for API Sandbox
            var sHeaders = {
                "Content-Type": "application/json",
                "APIKey": "My api Key"
            };

            //Available Security Schemes for productive API Endpoints
            //OAuth 2.0

            //sending request
            //API endpoint for API sandbox 
            var oData = {
                "sourceLanguage": langTo,
                "targetLanguages": [
                    langFrom
                ],
                "units": [{
                    "value": textOld,
                    "key": "ANALYZE_SALES_DATA"
                }]
            };

            oModel.loadData("https://sandbox.api.sap.com/ml/translation/translation", oData, true, "POST", null, false, sHeaders);

            //Available API Endpoints
            //https://mlfproduction-machine-translation.cfapps.eu10.hana.ondemand.com
            //https://mlfproduction-machine-translation.cfapps.us10.hana.ondemand.com

            //You can assign the created data model to a View and UI5 controls can be bound to it. Please refer documentation available at the below link for more information.
            //https://sapui5.hana.ondemand.com/#docs/guide/96804e3315ff440aa0a50fd290805116.html#loio96804e3315ff440aa0a50fd290805116

            //The below code snippet for printing on the console is for testing/demonstration purpose only. This must not be done in real UI5 applications.
            oModel.attachRequestCompleted(function (oEvent) {
                var oData = oEvent.getSource().oData;
                // console.log(oData);
            });

        }

我使用了两个 selectBox 来获取语言键,这两个调用 "idTo" 和 "idFrom"。而且我也使用了一个输入来获取文本将使用 id "idOldText" 进行翻译。但什么也没有发生。 oData 值在最后一条指令中始终为空。我使用的是 SAP WEBIDE,我想它不需要配置 IDE 来使用 API.

有人可以帮助我吗?

如果您从控制台提供错误信息,将会很有帮助。 但是我已经有一种感觉,这最终会导致跨站点请求,因此会因为使用完全限定的 URL 而被阻止。另外,您的 header 白名单可能丢失了。

像这样操作应该可以:

1) 在 SAP CP 中创建目标

2) 在 SAP WebIDE 中创建一个新的 sapui5 项目并通过添加新的目标路径和 header 将您的请求列入白名单来调整 neo-app.json headers

{
    "welcomeFile": "/webapp/index.html",
    "routes": [{
        "path": "/resources",
        "target": {
            "type": "service",
            "name": "sapui5",
            "entryPath": "/resources"
        },
        "description": "SAPUI5 Resources"
    }, {
        "path": "/test-resources",
        "target": {
            "type": "service",
            "name": "sapui5",
            "entryPath": "/test-resources"
        },
        "description": "SAPUI5 Test Resources"
    }, {
        "path": "/ml-dest",
        "target": {
            "type": "destination",
            "name": "sapui5ml-api"
        },
        "description": "ML API destination"
    }],
    "sendWelcomeFileRedirect": true,
    "headerWhiteList": [
        "APIKey", "Accept", "Content-Type"
    ]
}

3) 添加您的方法和 post 请求 ||您的版本中可能存在的问题:JSON object 和请求 headers

onInit: function () {

    var oModel = new sap.ui.model.json.JSONModel();

    var sHeaders = {
        "Content-Type": "application/json",
        "Accept": "application/json",
        "APIKey": "<<yourAPIKey>>"
    };

    var oData = {
        "sourceLanguage": "en",
        "targetLanguages": [
            "de"
        ],
        "units": [{
            "value": "I would like to analyze my sales data.",
            "key": "ANALYZE_SALES_DATA"
        }]
    };

    var ODataJSON = JSON.stringify(oData);

    oModel.loadData("/ml-dest/translation/translation", ODataJSON, true, "POST", null, false, sHeaders);
    oModel.attachRequestCompleted(function (oEvent) {
        var oData = oEvent.getSource().oData;
        console.log(oData.units[0].translations[0]);
    });

}

4) 在加载您的应用程序时获得成功响应 object :-)

使用的参考文献: