使用Microsoft Azure文本转换器API翻译示例文本的示例Java程序
我正试图通过Java程序使用Microsoft Azure文本转换器API将示例文本从一种语言翻译成另一种语言 请提供创建appId的步骤、Microsoft Azure文本转换器API的密钥以及将示例文本从一种语言转换为另一种语言的示例Java程序使用Microsoft Azure文本转换器API翻译示例文本的示例Java程序,java,azure,Java,Azure,我正试图通过Java程序使用Microsoft Azure文本转换器API将示例文本从一种语言翻译成另一种语言 请提供创建appId的步骤、Microsoft Azure文本转换器API的密钥以及将示例文本从一种语言转换为另一种语言的示例Java程序 提前感谢。根据我的理解,我认为您希望在Java程序中使用Microsoft DataMarket,但似乎不知道如何开始使用它 首先,您需要通过注册一个应用程序,以便在登录Microsoft DataMarket后获得客户端id&客户端机密 然
提前感谢。根据我的理解,我认为您希望在Java程序中使用Microsoft DataMarket,但似乎不知道如何开始使用它 首先,您需要通过注册一个应用程序,以便在登录Microsoft DataMarket后获得
客户端id
&客户端机密
然后,请参考以下文档了解如何获取access\u token
&翻译API
,您可以参考新文档的步骤12,转到“密钥”选项并复制订阅密钥以访问服务
//使用头传递密钥
curl--头“Ocp Apim订阅密钥:”--data“”
//使用查询字符串参数传递密钥
curl—数据“”“/Translate
HTTP接口,请参阅package sample;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import com.alibaba.fastjson.JSON;
public class TextTranslatorTest {
public static String getAccessToken(String charset, String clientId, String clientSecret, String scope,
String grantType) throws MalformedURLException, IOException {
String url = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
String paramsTemplate = "client_id=%s&client_secret=%s&scope=%s&grant_type=%s";
String params = String.format(paramsTemplate, URLEncoder.encode(clientId, charset),
URLEncoder.encode(clientSecret, charset), scope, grantType);
System.out.println(url);
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=" + charset);
conn.setRequestProperty("Accept-Charset", charset);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
IOUtils.write(params, conn.getOutputStream(), "UTF-8");;
String resp = IOUtils.toString(conn.getInputStream(), "UTF-8");
System.out.println(resp);
String accessToken = JSON.parseObject(resp).getString("access_token");
return accessToken;
}
public static String translate(String charset, String accessToken, String text, String from, String to) throws MalformedURLException, IOException {
String url = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + URLEncoder.encode(text, charset) + "&from=" + from + "&to=" + to;
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestProperty("Authorization", "Bearer" + " " + accessToken);
String resp = IOUtils.toString(conn.getInputStream(), "UTF-8");
return resp;
}
public static void main(String[] args) throws MalformedURLException, IOException {
String charset = StandardCharsets.UTF_8.name();
String clientId = "peter-translator-test";
String clientSecret = "xxxxxxxxxxxxxxxxxx";
String scope = "http://api.microsofttranslator.com";
String grantType = "client_credentials";
String accessToken = getAccessToken(charset, clientId, clientSecret, scope, grantType);
System.out.println(accessToken);
String text = "happy";
String from = "en";
String to = "de";
String resp = translate(charset, accessToken, text, from, to);
System.out.println(resp);
}
}