Microsoft Translator API Java,如何使用Azure获取客户端新ID

Translate.setClientId( “东西”); Translate.setClientSecret( “something1”);

我之前使用以下语法成功运行了我的代码,但是,有50%的时间我会收到错误消息: TranslateApiException:找不到与请求凭据关联的活动Azure市场转换器订阅。

我的应用程序订阅了Microsoft正在使用的OLD网站,但我认为问题正在发生,因为他们正在使用Azure。 现在,我的应用程序订阅了Azure,我订阅了Microsoft Translator API服务。 想知道如何将其设置为Azure提供的新ClientID,ClientSecret。

这是我首先订阅的“旧”网站: https : //datamarket.azure.com/home/

来自旧官方网站的信息(翻译语音和文本 api)和公告称,“ 微软翻译API现在可以在AZURE门户网站上使用 ”和“ 2017年4月30日之前需要采取的行动 – 微软翻译器迁移到Azure ”。 因此,如果您现在要使用Translator API,您需要拥有Azure订阅并创建Azure Cognitive服务的Translator帐户,如官方教程所述。

例如,使用Translator Text API,您可以按照新教程获取访问令牌,为API构建appid ,就像下面的Java中的示例代码一样。

 // Get the access token // The key got from Azure portal, please see https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-apis-create-account String key = ""; String authenticationUrl = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"; HttpsURLConnection authConn = (HttpsURLConnection) new URL(authenticationUrl).openConnection(); authConn.setRequestMethod("POST"); authConn.setDoOutput(true); authConn.setRequestProperty("Ocp-Apim-Subscription-Key", key); IOUtils.write("", authConn.getOutputStream(), "UTF-8"); String token = IOUtils.toString(authConn.getInputStream(), "UTF-8"); System.out.println(token); // Using the access token to build the appid for the request url String appId = URLEncoder.encode("Bearer "+token, "UTF-8"); String text = URLEncoder.encode("happy birthday", "UTF-8"); String from = "en"; String to = "fr"; String translatorTextApiUrl = String.format("https://api.microsofttranslator.com/v2/http.svc/Translate?appid=%s&text=%s&from=%s&to=%s", appId, text, from, to); HttpsURLConnection translateConn = (HttpsURLConnection) new URL(translatorTextApiUrl).openConnection(); translateConn.setRequestMethod("GET"); translateConn.setRequestProperty("Accept", "application/xml"); String resp = IOUtils.toString(translateConn.getInputStream(), "UTF-8"); System.out.println(resp); 

希望能帮助到你。 如有任何疑虑,请随时告诉我。

您可以通过https://www.microsoft.com/cognitive-services登录

然后,您将找到认知服务下所有服务的密钥列表: 在此处输入图像描述