简体   繁体   中英

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

I've been using Microsoft Translator service from my C# applications for a while. However, since I created my Azure subscription, the function below keeps returning: "Could not translate". As I understand, now I need to use Microsoft Translator somehow differently. I searched the Web actively but could not find any working examples or/and similar questions. Below I provide my code that used to work but now it does not:

        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;
       }

You use Microsoft translator in exactly the same way on Azure as you did before the move. The only difference is you need to use the Azure key and get a token from the new end point for tokens.

This shows you how to get an azure token:

https://github.com/MicrosoftTranslator/GetAzureToken

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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