与java应用程序集成时Bing转换器异常

时间:2017-03-30 10:43:15

标签: java azure bing-translator-api

我正在尝试在我的java应用程序中集成bing翻译器。我已注册到Microsoft azure认知服务和Microsoft市场。 执行下面的代码

import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;

public class Main {
    public static void main(String[] args) {
        try{
            Translate.setClientId(/* my Client Id */);
            Translate.setClientSecret(/* my Client Secret */);

            String translatedText = Translate.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH);

            System.out.println(translatedText);
        }
        catch(Exception e) {
            System.err.println("Exception: " + e.getMessage());
        }
    }
} 

我得到以下异常:

Page NoException in thread "main" java.lang.Exception: [microsoft-translator-api] Error retrieving translation : Server returned HTTP response code: 400 for URL: https://datamarket.accesscontrol.windows.net/v2/OAuth2-13
    at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:202)
    at com.memetix.mst.translate.Translate.execute(Translate.java:61)
    at test.SimpleExcelTranalator.main(SimpleExcelTranalator.java:44)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://datamarket.accesscontrol.windows.net/v2/OAuth2-13
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$10.run(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$10.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at com.memetix.mst.MicrosoftTranslatorAPI.getToken(MicrosoftTranslatorAPI.java:139)
    at com.memetix.mst.MicrosoftTranslatorAPI.retrieveResponse(MicrosoftTranslatorAPI.java:160)
    at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:199)
    ... 2 more
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://datamarket.accesscontrol.windows.net/v2/OAuth2-13
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
    at com.memetix.mst.MicrosoftTranslatorAPI.getToken(MicrosoftTranslatorAPI.java:138)
    ... 4 more

2 个答案:

答案 0 :(得分:1)

数据市场网址是您从中获取客户ID和秘密的地方。那是你发送翻译请求的地方吗?应该是这样的 http://api.microsofttranslator.com/v2/Http.svc/Translate

数据市场也被弃用了。您必须切换到Azure Cognitive Services http://docs.microsofttranslator.com/text-translate.html

答案 1 :(得分:0)

根据您在代码中引用的包com.memetix.mst.language.*,我搜索了GitHub上的源代码,Maven上的存储库以及GoogleCode上的旧存储库。我查看了它的源代码,发现它包含了来自Azure old datamarket web site的MS Translator Text API。该库已使用旧的REST API过时,旧的网站显示" MICROSOFT TRANSLATOR API现在可以在AZURE门户网站上使用"和" 重要:需要Azure帐户。阅读开始使用门户here的步骤。"首先,您需要Azure订阅才能在Azure门户上创建转换器文本API服务,然后编写代码以调用新的REST API。您可以参考我对其他两个SO线程的回答,了解如何使用新的REST API并通过我的示例代码调用它。

  1. 我对MS Translator returns empty response when used with Azure token的回答显示了新的REST API用法。
  2. 我对Microsoft Translator API Java, How to get client new ID with Azure的回答,包括我的示例代码,它展示了如何用Java调用新的REST API。
  3. 希望它有所帮助。