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

来自旧官方网站的信息(翻译

speech和

text api)&

Announcements说,“MICROSOFT TRANSLATOR API现在可以在AZURE门户网站上使用”和“2017年4月30日之前需要采取的行动 – 微软翻译器迁移到Azure”.因此,如果您现在要使用转换程序API,则需要拥有Azure订阅并创建Azure Cognitive服务的转换程序帐户,如官方

tutorial所述.

例如,使用Translator Text API,您可以按照新的tutorial获取访问令牌,为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);

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值