简体   繁体   English

天蓝色认知翻译服务的问题

[英]Issue with azure cognitive translation services

After getting the following code to work reliably for a month or so, it stopped working reliably a couple of days ago. 在使以下代码可靠运行一个月左右后,几天前它停止了可靠运行。 About half the time it returns a properly translated string and the other half of the time it returns one of the following two messages: 它大约有一半的时间返回正确翻译的字符串,而另一半则返回以下两个消息之一:

java.io.FileNotFoundException: https://api.cognitive.microsoft.com/sts/v1.0/issueToken java.io.FileNotFoundException: https : //api.cognitive.microsoft.com/sts/v1.0/issueToken

java.net.UnknownHostException: Unable to resolve host "api.microsofttranslator.com": No address associated with hostname java.net.UnknownHostException:无法解析主机“ api.microsofttranslator.com”:没有与主机名关联的地址

The timing of this problem's beginning coincided with the expiration of my free azure cognitive services account however I migrated to a pay-as-you-go account yesterday and the problem continues. 这个问题开始的时间恰好是我的免费Azure认知服务帐户到期的时间,但是我昨天迁移到了“现收现付”帐户,问题仍然存在。

Why is this happening? 为什么会这样呢?

static class translateMessageX extends AsyncTask<String, Void, String>
{
    //input string array of 3 items
    //[0]is the message to be translated
    //[1]is the from language i.e. "english"
    //[2]is the to language i.e. "spanish"
    //[3]"echo" or "received"
    String retString;
    String inString = null;
    String messageType = null;
    String URLHolder = "";  //hold the URL here while we are translating the text
    @Override
    protected String doInBackground(String... params)
    {
        inString = params[0];
        String from = params[1];
        String to = params[2];
        messageType = params[3];
        int urlStart = inString.indexOf("http");
        if (!(urlStart == -1))
        {
            URLHolder = inString.substring(urlStart);
            inString = inString.substring(0, urlStart -1);
        }
        else
        {
            URLHolder = "";
        }


        Integer mesChars = params[0].length();
        Integer tCharsLeft = GlobalStuff.getTranslationsFromSP();
        if (tCharsLeft > 0)
        {
            if (tCharsLeft < mesChars)  //we charge for both 'echo' and 'received' translations
            {
                GlobalStuff.updateTranslationInventory(tCharsLeft * -1);
            }
            else
            {
                GlobalStuff.updateTranslationInventory(mesChars * -1);
            }
            GlobalStuff.notifyListeners(this, "#uui", "notused", "notused" );
            try
            {

                Language fromLang = GlobalStuff.getLang(from);
                Language toLang = GlobalStuff.getLang(to);

                //retString = Translate.execute(inString, fromLang, toLang);
                //String debugstr = "look at retStr";
                String authenticationUrl = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";
                HttpsURLConnection authConn = (HttpsURLConnection) new URL(authenticationUrl).openConnection();
                authConn.setRequestMethod("POST");
                authConn.setDoOutput(true);
                authConn.setRequestProperty("Ocp-Apim-Subscription-Key", GlobalStuff.translateKey);
                IOUtils.write("", authConn.getOutputStream(), "UTF-8");
                String token = IOUtils.toString(authConn.getInputStream(), "UTF-8");
                System.out.println(token);
                // Using the access token to build the appid for the request url
                String appId = URLEncoder.encode("Bearer "+token, "UTF-8");
                String text = URLEncoder.encode(inString, "UTF-8");
                String translatorTextApiUrl = String.format("https://api.microsofttranslator.com/v2/http.svc/Translate?appid=%s&text=%s&from=%s&to=%s", appId, text, fromLang, toLang);
                HttpsURLConnection translateConn = (HttpsURLConnection) new URL(translatorTextApiUrl).openConnection();
                translateConn.setRequestMethod("GET");
                translateConn.setRequestProperty("Accept", "application/xml");
                retString = IOUtils.toString(translateConn.getInputStream(), "UTF-8");
                String debug = "look at retString";
            }

            catch (Exception e)
            {
                retString = e.toString();
            }
        }
        else
        {
            retString = "OUT OF TRANSLATION CREDITS - " + inString;
        }

        return retString;

    }

    @Override
    protected void onPostExecute(String result)
    {
        //rest of logic should be here??

        String debug = "look at result";
        String answer = extractTranslation(result);
    .. . . .

Host not found looks like a simple connectivity error. 找不到主机看起来像是简单的连接错误。 These hosts do exist. 这些主机确实存在。

You can void the call to the token service by passing the key in the call to api.microsofttranslator.com directly: https://cognitive.uservoice.com/knowledgebase/articles/1815385-api-translator-text-speech-using-the-api-key 您可以通过直接将调用中的密钥传递给api.microsofttranslator.com来使对令牌服务的调用无效: https ://cognitive.uservoice.com/knowledgebase/articles/1815385-api-translator-text-speech-using- api键

That fixes one of the host not found problems, but not the other. 这样可以修复主机中未发现问题的主机,但不能修复另一主机。

I would recommend though to not embed the key in the client application. 我建议尽管不要在客户端应用程序中嵌入密钥。 It is safer to call the translator service from your own proxy service, where the proxy is able to safely identify your client as your client. 从您自己的代理服务调用翻译器服务会更安全,代理可以安全地将您的客户端标识为您的客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM