簡體   English   中英

天藍色認知翻譯服務的問題

[英]Issue with azure cognitive translation services

在使以下代碼可靠運行一個月左右后,幾天前它停止了可靠運行。 它大約有一半的時間返回正確翻譯的字符串,而另一半則返回以下兩個消息之一:

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

java.net.UnknownHostException:無法解析主機“ api.microsofttranslator.com”:沒有與主機名關聯的地址

這個問題開始的時間恰好是我的免費Azure認知服務帳戶到期的時間,但是我昨天遷移到了“現收現付”帳戶,問題仍然存在。

為什么會這樣呢?

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);
    .. . . .

找不到主機看起來像是簡單的連接錯誤。 這些主機確實存在。

您可以通過直接將調用中的密鑰傳遞給api.microsofttranslator.com來使對令牌服務的調用無效: https ://cognitive.uservoice.com/knowledgebase/articles/1815385-api-translator-text-speech-using- api鍵

這樣可以修復主機中未發現問題的主機,但不能修復另一主機。

我建議盡管不要在客戶端應用程序中嵌入密鑰。 從您自己的代理服務調用翻譯器服務會更安全,代理可以安全地將您的客戶端標識為您的客戶端。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM