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.

Wednesday, April 18, 2012

Linux shell: stop ctrl+s

in a Linux shell, when you press ctrl+s, your shell will be suspended.
This may not be what you like.
To disable this feature, you can add one line to your .bashrc file in your home directory:

stty –ixon