Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

Setting up and using Bing Translate API Service for Machine Translation


:P
On this page:

Last week I spent quite a bit of time trying to set up the Bing Translate API service. I can honestly say this was one of the most screwed up developer experiences I've had in a long while - specifically related to the byzantine sign up process that Microsoft has in place. Not only is it nearly impossible to find decent documentation on the required signup process, some of the links in the docs are just plain wrong, and some of the account pages you need to access the actual account information once signed up are not linked anywhere from the administration UI. To make things even harder is the fact that the APIs changed a while back, with a completely new authentication scheme that's described and not directly linked documentation topic also made for a very frustrating search experience.

It's a bummer that this is the case too, because the actual API itself is easy to use and works very well - fast and reasonably accurate (as accurate as you can expect machine translation to be). But the sign up process is a pain in the ass doubtlessly leaving many people giving up in frustration.

In this post I'll try to hit all the points needed to set up to use the Bing Translate API in one place since such a document seems to be missing from Microsoft. Hopefully the API folks at Microsoft will get their shit together and actually provide this sort of info on their site…

Signing Up

The first step required is to create a Windows Azure MarketPlace account.

Go to:

If you don't have an account you will be taken to a registration page which you have to fill out. Follow the links and complete the registration.

Once you're signed in you can start adding services.

  • Click on the Data Link on the main page
  • Select Microsoft Translator from the list

This adds the Microsoft Bing Translator to your services.

Pricing

The page shows the pricing matrix and the free service which provides 2 megabytes for translations a month for free. Prices go up steeply from there. Pricing is determined by actual bytes of the result translations used. Max translations are 1000 characters so at minimum this means you get around 2000 translations a month for free. However most translations are probable much less so you can expect larger number of translations to go through. For testing or low volume translations this should be just fine.

Once signed up there are no further instructions and you're left in limbo on the MS site.

Register your Application

Once you've created the Data association with Translator the next step is registering your application. To do this you need to access your developer account.

  • Go to https://datamarket.azure.com/developer/applications/register
  • Provide a ClientId, which is effectively the unique string identifier for your application (not your customer id!)
  • Provide your name
  • The client secret was auto-created and this becomes your 'password'
  • For the redirect url provide any https url: https://microsoft.com works
  • Give this application a description of your choice so you can identify it in the list of apps

Now, once you've registered your application, keep track of the ClientId and ClientSecret - those are the two keys you need to authenticate before you can call the Translate API.

Oddly the applications page is hidden from the Azure Portal UI. I couldn't find a direct link from anywhere on the site back to this page where I can examine my developer application keys. To find them you can go to:

https://datamarket.azure.com/developer/applications

You can come back here to look at your registered applications and pick up the ClientID and ClientSecret.

Fun eh? But we're now ready to actually call the API and do some translating.

Using the Bing Translate API

The good news is that after this signup hell, using the API is pretty straightforward. To use the translation API you'll need to actually use two services: You need to call an authentication API service first, before you can call the actual translator API. These two APIs live on different domains, and the authentication API returns JSON data while the translator service returns XML. So much for consistency.

Authentication

The first step is authentication. The service uses oAuth authentication with a  bearer token that has to be passed to the translator API. The authentication call retrieves the oAuth token that you can then use with the translate API call. The bearer token has a short 10 minute life time, so while you can cache it for successive calls, the token can't be cached for long periods. This means for Web backend requests you typically will have to authenticate each time unless you build a more elaborate caching scheme that takes the timeout into account (perhaps using the ASP.NET Cache object). For low volume operations you can probably get away with simply calling the auth API for every translation you do.

To call the Authentication API use code like this:

/// 
/// Retrieves an oAuth authentication token to be used on the translate
/// API request. The result string needs to be passed as a bearer token
/// to the translate API.
/// 
/// You can find client ID and Secret (or register a new one) at:
/// https://datamarket.azure.com/developer/applications/
/// 
/// The client ID of your application
/// The client secret or password
/// 
public string GetBingAuthToken(string clientId = null, string clientSecret = null)
{
    string authBaseUrl = https://datamarket.accesscontrol.windows.net/v2/OAuth2-13;

    if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret))
    {
        ErrorMessage = Resources.Resources.Client_Id_and_Client_Secret_must_be_provided;
        return null;
    }
            
    var postData = string.Format("grant_type=client_credentials&client_id={0}" +
                                    "&client_secret={1}" +
                                    "&scope=http://api.microsofttranslator.com",
                                    HttpUtility.UrlEncode(clientId),
                                    HttpUtility.UrlEncode(clientSecret));

    // POST Auth data to the oauth API
    string res, token;

    try
    {
        var web = new WebClient();
        web.Encoding = Encoding.UTF8;
        res = web.UploadString(authBaseUrl, postData);
    }
    catch (Exception ex)
    {
        ErrorMessage = ex.GetBaseException().Message;
        return null;
    }

    var ser = new JavaScriptSerializer();
    var auth = ser.Deserialize<BingAuth>(res);
    if (auth == null)
        return null;
            
    token = auth.access_token;

    return token;
}


private class BingAuth
{
    public string token_type { get; set; }
    public string access_token { get; set; }
}

This code basically takes the client id and secret and posts it at the oAuth endpoint which returns a JSON string. Here I use the JavaScript serializer to deserialize the JSON into a custom object I created just for deserialization. You can also use JSON.NET and dynamic deserialization if you are already using JSON.NET in your app in which case you don't need the extra type. In my library that houses this component I don't, so I just rely on the built in serializer.

The auth method returns a long base64 encoded string which can be used as a bearer token in the translate API call.

Translation

Once you have the authentication token you can use it to pass to the translate API. The auth token is passed as an Authorization header and the value is prefixed with a 'Bearer ' prefix for the string.

Here's what the simple Translate API call looks like:

/// 
/// Uses the Bing API service to perform translation
/// Bing can translate up to 1000 characters. 
/// 
/// Requires that you provide a CLientId and ClientSecret
/// or set the configuration values for these two.
/// 
/// More info on setup:
/// http://www.west-wind.com/weblog/
/// 
/// Text to translate
/// Two letter culture name
/// Two letter culture name
/// Pass an access token retrieved with GetBingAuthToken.
/// If not passed the default keys from .config file are used if any
/// 
public string TranslateBing(string text, string fromCulture, string toCulture,
                            string accessToken = null)
{
    string serviceUrl = "http://api.microsofttranslator.com/V2/Http.svc/Translate";

    if (accessToken == null)
    {
        accessToken = GetBingAuthToken();
        if (accessToken == null)
            return null;
    }
            
    string res;

    try
    {
        var web = new WebClient();                
        web.Headers.Add("Authorization", "Bearer " + accessToken);                        
        string ct = "text/plain";
        string postData = string.Format("?text={0}&from={1}&to={2}&contentType={3}",
                                    HttpUtility.UrlEncode(text),
                                    fromCulture, toCulture,
                                    HttpUtility.UrlEncode(ct));

        web.Encoding = Encoding.UTF8;
        res = web.DownloadString(serviceUrl + postData);
    }
    catch (Exception e)
    {
        ErrorMessage = e.GetBaseException().Message;
        return null;
    }

    // result is a single XML Element fragment
    var doc = new XmlDocument();
    doc.LoadXml(res);            
    return doc.DocumentElement.InnerText;          
}

The first of this code deals with ensuring the auth token exists. You can either pass the token into the method manually or let the method automatically retrieve the auth code on its own. In my case I'm using this inside of a Web application and in that situation I simply need to re-authenticate every time as there's no convenient way to manage the lifetime of the auth cookie.

The auth token is added as an Authorization HTTP header prefixed with 'Bearer ' and attached to the request. The text to translate, the from and to language codes and a result format are passed on the query string of this HTTP GET request against the Translate API.

The translate API returns an XML string which contains a single element with the translated string.

Using the Wrapper Methods

It should be pretty obvious how to use these two methods but here are a couple of test methods that demonstrate the two usage scenarios:

[TestMethod]
public void TranslateBingWithAuthTest()
{
    var translate = new TranslationServices();

    string clientId = DbResourceConfiguration.Current.BingClientId;
    string clientSecret = DbResourceConfiguration.Current.BingClientSecret;

    string auth = translate.GetBingAuthToken(clientId, clientSecret);
     Assert.IsNotNull(auth);

    string text = translate.TranslateBing("Hello World we're back home!", "en", "de",auth);
    Assert.IsNotNull(text, translate.ErrorMessage);
    Console.WriteLine(text);
}


[TestMethod]
public void TranslateBingIntegratedTest()
{
    var translate = new TranslationServices();
    string text = translate.TranslateBing("Hello World we're back home!","en","de");
    Assert.IsNotNull(text, translate.ErrorMessage);
    Console.WriteLine(text);
}

Other API Methods

The Translate API has a number of methods available and this one is the simplest one but probably also the most common one that translates a single string.

You can find additional methods for this API here:

http://msdn.microsoft.com/en-us/library/ff512419.aspx

Soap and AJAX APIs are also available and documented on MSDN:

http://msdn.microsoft.com/en-us/library/dd576287.aspx

These links will be your starting points for calling other methods in this API.

Dual Interface

I've talked about my database driven localization provider here in the past, and it's for this tool that I added the Bing localization support. Basically I have a localization administration form that allows me to translate individual strings right out of the UI, using both Google and Bing APIs:

WebTranslation

As you can see in this example, the results from Google and Bing can vary quite a bit - in this case Google is stumped while Bing actually generated a valid translation. At other times it's the other way around - it's pretty useful to see multiple translations at the same time. Here I can choose from one of the values and driectly embed them into the translated text field.

Lost in Translation

There you have it. As I mentioned using the API once you have all the bureaucratic crap out of the way calling the APIs is fairly straight forward and reasonably fast, even if you have to call the Auth API for every call.

Hopefully this post will help out a few of you trying to navigate the Microsoft bureaucracy, at least until next time Microsoft upends everything and introduces new ways to sign up again. Until then - happy translating…

Related Posts

Posted in Localization  ASP.NET  .NET  

The Voices of Reason


 

Andrei Rinea
June 06, 2013

# re: Setting up and using Bing Translate API Service for Machine Translation

Excellent post as always! I did go through similar issues when dealing with Bing but things seemed to be <a href="http://blog.andrei.rinea.ro/2013/06/01/bing-it-on-reactive-extensions-story-code-and-slides/">easier</>.

Knaģis
June 06, 2013

# re: Setting up and using Bing Translate API Service for Machine Translation

Are you sure about the 1000 character limit? When I developed against the Translator API year or so ago, the limit was 10240 characters - have they decreased the limit? I usually use it for very short strings so I might not have noticed if they decreased the limit.

Rick Strahl
June 06, 2013

# re: Setting up and using Bing Translate API Service for Machine Translation

@Knaģis - re: 1000 char limit: That's what the docs say. It's also not clear whether that's for the actual string or the entire translation structure returned which is a bit more verbose. I'm pretty sure that's accurate though - I've had longer queries just cut off. Google is even worse - it seems to cut of after the first sentence.

SteveIsNotLive
September 10, 2014

# re: Setting up and using Bing Translate API Service for Machine Translation

Great stuff as usual. Thanks for sharing!

You were able to login to a Live service without requiring a user to add a name/pw via the Live logon GUI and get the auth token. I stumbled across this post looking for a way to do the same with the Bing Ads API, but it apparently not possible - have you seen a way to automate MS's OAuth2 implementation with WebClient for Bing Ads?

Thanks again, been respecting your stuff since my days of FP 2.6.

Mario Vernari
April 17, 2015

# re: Setting up and using Bing Translate API Service for Machine Translation

Rick, ask MS to thank you, because I was throwing all the code in the can.
I really couldn't agree more on the blurry yet few-documented APIs. I needed a trivial C# Console example, but the only one I've found it looks obsolete.
You gave me (us) some very useful tip on how to use those APIs.

Maybe it's worthwhile to mention some trouble I've found related to "strange" characters in the string. In the specific, I tried to send a Greek-delta sign in the string, but it is always treated as a "?".
Hints?

Thank you anyway and good luck!

Rick Strahl
April 17, 2015

# re: Setting up and using Bing Translate API Service for Machine Translation

@Mario - the data is JSON and UTF-8 encoded. The delta sign is part of a UTF-8 sequence to represent an extended character. Not sure how you're capturing the data, but if you're using an HTTP client that doesn't automatically decode data you'll need to do the UTF-8 decoding explicitly.

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024