Microsoft Translator API Java, How to get client new ID with Azure

[亡魂溺海] 提交于 2019-11-27 07:18:59

问题


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

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. :

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.

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


回答1:


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.

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.




回答2:


You can to sign in through https://www.microsoft.com/cognitive-services

Then, you'll find a list of keys for all services under cognitive services:



来源:https://stackoverflow.com/questions/42891510/microsoft-translator-api-java-how-to-get-client-new-id-with-azure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!