Microsoft Translator API Java,如何使用 Azure 获取客户端新 ID [英] Microsoft Translator API Java, How to get client new ID with Azure

查看:37
本文介绍了Microsoft Translator API Java,如何使用 Azure 获取客户端新 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Translate.setClientId("something");Translate.setClientSecret("something1");

Translate.setClientId("something"); Translate.setClientSecret("something1");

我之前使用以下语法成功运行了我的代码,但是,有 50% 的时间我会收到一条错误消息:TranslateApiException:找不到与请求凭据关联的活动 Azure Market Place Translator Subscription.:

I had previously ran my code successfully using the following syntax, however, 50% of the time I will get an error saying: TranslateApiException: Cannot find an active Azure Market Place Translator Subscription associated with the request credentials. :

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

I had my app subscribed with the OLD website that Microsoft was using, but I think the problem is occurring because they are using Azure. Now, I have my app subscribed with Azure, I have a subscription for the Microsoft Translator API services. Was wondering how to set this to the NEW ClientID, ClientSecret that Azure provides.

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

This is the "old" site that I subscribed through first: https://datamarket.azure.com/home/

推荐答案

如旧官网资料(译者语音 & text api) &公告说,<强>微软翻译器 API 现在可在 AZURE 门户上使用"和需要在 2017 年 4 月 30 日之前采取行动 - 微软翻译器移至 Azure".因此,如果您现在想使用 Translator API,则需要拥有 Azure 订阅并创建 Azure Cognitive 服务的 Translator 帐户,例如官方 教程 说.

As the information from the old offical site(for translator speech & text api) & Announcements said, "THE MICROSOFT TRANSLATOR API IS NOW AVAILABLE ON THE AZURE PORTAL" and "Action Required before April 30, 2017 - Microsoft Translator Moves to Azure". So if you want to use the Translator API now, you need to have an Azure subscription and create a Translator account of Azure Cognitive service like the offical tutorial said.

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

For example using Translator Text API, you can follow the new tutorial to get an access token to build an appid for the API like my sample code in Java below.

// 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 = "<your translator account 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);

希望有帮助.有任何问题,请随时告诉我.

Hope it helps. Any concern, please feel free to let me know.

这篇关于Microsoft Translator API Java,如何使用 Azure 获取客户端新 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆