繁体   English   中英

Azure上的Microsoft Translator服务(如何使其工作?)

[英]Microsoft Translator service on Azure (how to make it work?)

我已经在C#应用程序中使用Microsoft Translator服务一段时间了。 但是,由于我创建了Azure订阅,因此以下函数不断返回:“无法翻译”。 据我了解,现在我需要以某种方式使用Microsoft Translator。 我在网上积极搜索,但是找不到任何可行的示例或类似的问题。 下面,我提供了以前可以正常工作的代码,但现在不起作用了:

        public static String TranslateToEnglish(String str)
        {
             return Translate(GetTokenWrapper(), str, "en");
        }

        public static String GetTokenWrapper()
        {
             AdmAccessToken admToken;

             AdmAuthentication admAuth = new AdmAuthentication("..", "..");
             admToken = admAuth.GetAccessToken();
             return "Bearer " + admToken.access_token;


        }

       public static String Translate(HttpRequestMessageProperty httpRequestProperty,string authToken, string what, string to)
       {
            // Add TranslatorService as a service reference, Address:http://api.microsofttranslator.com/V2/Soap.svc
            LanguageServiceClient client = new LanguageServiceClient();
            HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
            httpRequestProperty.Method = "POST";
            httpRequestProperty.Headers.Add("Authorization", authToken);
            using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
            {
                  OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;

                  string sourceText = what;

                  string translationResult;

                  try 
                  { 
                       translationResult = client.Translate("", sourceText, "", to, "text/plain", "general", ""); 
                  }
                  catch(Exception ex) { return ex.ToString(); }
            return translationResult;
       }

您在Microsoft Azure上的使用方式与迁移之前完全相同。 唯一的区别是您需要使用Azure密钥并从新端点获取令牌的令牌。

这向您展示了如何获取蔚蓝令牌:

https://github.com/MicrosoftTranslator/GetAzureToken

暂无
暂无

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

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