Thursday, April 26, 2012

How to use the new Bing translator API with access tokens

Bing translator has changed its API recently, and it forces developers to use a more complicated way than the previous way using AppID.

The new API involves a temporal token, named as access token, which will expire in 10 minutes after you get it.

The detailed steps of using the new API with access tokens are as follows (all the parameters for the curl command should be used after URL encode):

(1) sign up for API access at Azure Data Market, from which you can get your Client Secret (a string, in My Account -> Account Keys);

(2) register the client application (that is, the app using the API), where you can create your own Client ID and Name, and you also have to type in redirect URI which should be a valid URL address (like "http://www.....");

(3)  to get an access token, make a POST request to:
https://datamarket.accesscontrol.windows.net/v2/OAuth2-13
For example,
curl -d 'grant_type=client_credentials&client_id=Client ID&client_secret=Client  Secret&scope=http://api.microsofttranslator.com' https://datamarket.accesscontrol.windows.net/v2/OAuth2-13 | python -m json.tool
The returned data is in JSON format, in which you can find your temporal access token (a string, which is the value of the key "access_token").

(4) make POST requests to the Bing translator API with the access token to ask Bing to translate texts;
For example,
curl -H 'Authorization:bearer Your-Access-Token'  'http://api.microsofttranslator.com/V2/Http.svc/Translate?text=hello&from=en&to=zh-CHS'

(5) go back to (3), if the access token expires after 10 minutes, and we need to renew it again every 10 minutes.

11 comments:

Translator Jobs said...

great, the bing API is useful.

Anonymous said...

Thanks, it helped me build my tiny application.

trikthemaster said...

hello, could you provide php code from requesting token, until getting the translation result?

thanks in advance

Upeksha Wisidagama said...

Very clear instructions. Thanks Wang.

biovamps said...

http://code.google.com/p/micrsoft-translator-php-wrapper/
and you can see the demo also
http://renjith.co.in/translate/

AD said...

Wang, thanks for providing clarity. I'm able to get the access_token (using perl). My access_token is a very large string, starts with http and contains "%" characters ..its kind of a big http url.
Is that how it should be?

Thanks!

trikthemaster said...

I tried the code from: http://code.google.com/p/micrsoft-translator-php-wrapper/

I believe it doesn't have script to renew the token. I visited the demo too, and it happened to work only for several minutes.

Valerio Morgante said...

make a translation, is a GET request

Marco said...

https://sites.google.com/site/reformascubiertastejadosmadrid/


masina s-a stricat, trebuie s-o repar

Anonymous said...

Can you post how to do the same with web searches. thx dude

Anonymous said...

omg this info is pointless for noobs (me me me me me). it's all so bloody confusing :(

:D