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

时间:2017-01-30 18:20:13

标签: c# azure

我已经在我的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;
       }

1 个答案:

答案 0 :(得分:0)

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

这将向您展示如何获得天蓝色标记:

https://github.com/MicrosoftTranslator/GetAzureToken