微软Bing翻译API的使用

首先,可以到http://code.google.com/p/microsoft-translator-java-api/这里下载Java类库。它有两种类型,一个是包含所有依赖的,一个是不包含依赖的。我下载的是包含依赖的,方便好用。

然后,在到这里https://datamarket.azure.com/account 取得你的clientid和secret。当然,前面你会经过一系列的注册登陆等流程。弄好之后,再点击这个页面就能看到如下的东西了:

16

上图中的“主帐户密钥”和“客户 ID”就是我们要的了。

然后按照那个翻译Java类库的代码,把id和密钥填入,如下,就能实现翻译了:

import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;
public class Main {
  public static void main(String[] args) throws Exception {
// Set your Windows Azure Marketplace client info - See http://msdn.microsoft.com/en-us/library/hh454950.aspx
     Translate.setClientId(/* Enter your Windows Azure Client Id here */);
     Translate.setClientSecret(/* Enter your Windows Azure Client Secret here */);
     String translatedText = Translate.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH);
    System.out.println(translatedText);
  }
-------------

后续更新,用了几次之后,发现出现了这样的异常:

TranslateApiException: Cannot find an active Azure Market Place Translator Subscription associated with the request credentials. : ID=1035.V2_Json.Translate.227EAA79

谷歌搜索了一下,才知道像我上面用主账号的id和密钥是不行的。到https://datamarket.azure.com/developer/applications 这里注册一个应用,应用的id就是你给它起的名字,最好是中文,然后密钥会自动给你。用这对就不会出现这样的问题了

猜你喜欢

转载自luotianwen456123.iteye.com/blog/2219690