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

Microsoft Translator API Java,如何使用Azure获取客户端新ID,java,azure,clientid,Java,Azure,Clientid,setClientId(“某物”); setClientSecret(“something1”) 我以前曾使用以下语法成功运行过代码,但有50%的时间我会收到一条错误消息:translateAppiexception:找不到与请求凭据关联的活动Azure Market Place Translator订阅: 我用微软使用的旧网站订阅了我的应用程序,但我认为问题的出现是因为他们使用的是Azure。现在,我已经向Azure订阅了我的应用程序,我已经订阅了Microsoft Translator AP

setClientId(“某物”); setClientSecret(“something1”)

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

我用微软使用的旧网站订阅了我的应用程序,但我认为问题的出现是因为他们使用的是Azure。现在,我已经向Azure订阅了我的应用程序,我已经订阅了Microsoft Translator API服务。我想知道如何将其设置为Azure提供的新ClientID和ClientSecret

这是我首先订阅的“旧”网站:

您可以通过

然后,您将在“认知服务”下找到所有服务的密钥列表:

正如旧的官方网站(用于translator&api)中的信息所说,“MICROSOFT translator api现在可以在AZURE门户上使用”,“2017年4月30日之前需要执行的操作-MICROSOFT translator移动到AZURE”。因此,如果你想现在就使用Translator API,你需要有一个Azure订阅,并创建Azure认知服务的翻译帐户,就像这位官员所说的那样

例如,使用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 = "<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);
//获取访问令牌
//密钥已从Azure门户获取,请参阅https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-apis-create-account
字符串键=”;
字符串身份验证URL=”https://api.cognitive.microsoft.com/sts/v1.0/issueToken";
HttpsURLConnection authConn=(HttpsURLConnection)新URL(authenticationUrl).openConnection();
authConn.setRequestMethod(“POST”);
authConn.setDoOutput(真);
setRequestProperty(“Ocp Apim订阅密钥”,密钥);
write(“,authConn.getOutputStream(),“UTF-8”);
字符串标记=IOUtils.toString(authConn.getInputStream(),“UTF-8”);
System.out.println(令牌);
//使用访问令牌为请求url生成appid
字符串appId=URLEncoder.encode(“载体”+令牌,“UTF-8”);
字符串text=urlcoder.encode(“生日快乐”,“UTF-8”);
字符串from=“en”;
字符串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)新URL(translaterExtapiURL).openConnection();
translatecon.setRequestMethod(“GET”);
setRequestProperty(“接受”、“应用程序/xml”);
String resp=IOUtils.toString(translatecon.getInputStream(),“UTF-8”);
系统输出打印项次(resp);

希望能有帮助。如果有任何问题,请随时告诉我。

伙计,你救了我。谢谢。