Text Translation: Migrating from Microsoft Datamarket to Azure Cognitive Services

Microsoft translation APIs moved from Microsoft DataMarket to Microsoft Azure Portal. Here is the announcements on the same.

Microsoft Translator Text and Speech Translation APIs are now available in the Azure portal under the Cognitive Services category. Existing applications using Microsoft Translator need to be changed before April 30, 2017.

Cognitive Services are new home for translation APIs. Here is a snapshot

 

image

 

For getting started-up on new Azure Translation APIs please follow steps on: https://www.microsoft.com/en-us/translator/getstarted.aspx

Here is step by step procedure by Adding Microsoft Translator subscription to your Azure account

  1. Log into https://portal.azure.com and select Cognitive Services:

Select “+” and search for “Cognitive Search APIs”. Fill in all the fields

image — >   image —> image –>image

 

Voila!! you have just created an account for calling Translation APIs RESTfully.

The account you just created will now look like below snapshot. We are interested in Keys section. Select keys and copy Key1 as below: This key will be used as Subscription-Key or Ocp-Apim-Subscription-Key

image ——-> image

Now the fun part: Lets do some translations using Postman: You can always get token id from page: http://docs.microsofttranslator.com/oauth-token.html

First; lets fetch

DataMarket api WAS:

url: https://datamarket.accesscontrol.windows.net/v2/OAuth2-13

Content-Type: application/x-www-form-urlencoded

payload: grant_type=client_credentials&client_id=<clientID>&client_secret=<clientsecret>&scope=http://api.microsofttranslator.com

image

The NEW way to fetch access token using Azure cognitive APIs is

Action: POST

url: https://api.cognitive.microsoft.com/sts/v1.0/issueToken?Subscription-Key=<subscription-key>

Content-Type: application/xml

Ocp-Apim-Subscription-Key : <subscription-key>

payload: <null>

PS: You can either send subscription-key in url OR send it as Ocp-Apim-Subscription-Key in header.

image

Now lets translate some text:

Action: POST

url: http://api.microsofttranslator.com/v2/Http.svc/TranslateArray

Headers: Authorization Bearer <Key returned from issueToken API>

Content-Type: application/xml

Payload:

image

output:

image

Here is some C# code for the same:

public static async Task<String> GetToken()
{

string clientID = ConfigurationManager.AppSettings[“ClientID”].ToString();
string clientSecret =  ConfigurationManager.AppSettings[“ClientSecret”].ToString();
Uri translatorAccessURI = new Uri(“https://api.cognitive.microsoft.com/sts/v1.0/issueToken?Subscription-Key=<YOUR SUBSCRIPTION KEY>”);//new Uri(“https://datamarket.accesscontrol.windows.net/v2/OAuth2-13″);

//// Create form parameters that we will send to data market.
//Dictionary<string, string> requestDetails = new Dictionary<string, string>
//    {
//        { “grant_type”, “client_credentials” },
//        { “client_id”,   clientID},
//        { “client_secret”,  clientSecret },
//        { “scope”, “http://api.microsofttranslator.com”/ }
//    };

//FormUrlEncodedContent requestContent = new FormUrlEncodedContent(requestDetails, Encoding.UTF8, “application/xml”);
HttpContent requestContent = new StringContent(“”, Encoding.UTF8, “application/xml”);
requestContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(“application/xml”);
//Ocp-Apim-Subscription-Key

// We use a HttpClient instance for Azure Marketplace request
HttpClient client = new HttpClient();

//send to data market
HttpResponseMessage dataMarketResponse = await client.PostAsync(translatorAccessURI, requestContent);

// If client authentication failed then we get a JSON response from Azure Market Place
if (!dataMarketResponse.IsSuccessStatusCode)
{
JToken error = await dataMarketResponse.Content.ReadAsAsync<JToken>();
string errorType = error.Value<string>(“error”);
string errorDescription = error.Value<string>(“error_description”);
throw new HttpRequestException(string.Format(“Azure market place request failed: {0} {1}”, errorType, errorDescription));
}

// Get the access token to attach to the original request from the response body
string token = await dataMarketResponse.Content.ReadAsStringAsync();//<JToken>();
// string token = response.Value<string>();// “access_token”);// HttpUtility.UrlEncode(response.Value<string>(“access_token”));

return token;
}

So, now you can now expect all the Azure goodness in Translations as well.

Happy translation!!

खुश अनुवाद!   Glücklich Übersetzung!!    快乐的翻译!!   幸せな翻訳!   ترجمه مبارک!    Счастливый перевод!   ¡Traducción feliz!!!!    خوش ترجمہ!!    Traduction heureuse !!    Traduzione felice!!