我正在尝试使用Microsoft Azure Text Translator API通过Java程序将示例文本从一种语言翻译成另一种语言。
请为我提供步骤,为Microsoft Azure Text Translator API创建appId,Secret密钥,并提供示例Java程序,将示例文本从一种语言转换为另一种语言。
提前致谢。
答案 0 :(得分:1)
根据我的理解,我认为您希望在Java程序中使用Microsoft DataMarket中的Microsoft Translator - Text Translation
,但似乎不知道如何开始使用它。
首先,您需要通过https://datamarket.azure.com/developer/applications/register注册申请才能获得client_id
&登录Microsoft DataMarket后client_secret
然后,请参阅以下文档,了解如何获取access_token
&翻译API。
作为参考,这是我的Java库示例代码apache commons-io
& fastjson
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);
}
}
请注意,根据页面https://translatorbusiness.uservoice.com/knowledgebase/articles/1078534-microsoft-translator-on-azure,您只需在2017年4月30日之前使用上述答案。然后,您需要按照新文档http://docs.microsofttranslator.com/text-translate.html在Azure上使用Text Translator API。但是Azure上的Text Translator的新服务似乎还没有通过我的测试准备好。所以我只列出以下简单步骤作为参考。
获取Azure访问令牌,请参阅http://docs.microsofttranslator.com/oauth-token.html。要获得<your-key>
,您可以参考新文档的第12步到Go to the Keys option and copy your subscription key to access the service
。
//使用标头传递密钥 curl --header'Ocp-Apim-Subscription-Key:' - data“”'https://api.cognitive.microsoft.com/sts/v1.0/issueToken' //使用查询字符串参数传递密钥 curl --data“”'https://api.cognitive.microsoft.com/sts/v1.0/issueToken?Subscription-Key='
要拨打/Translate
HTTP接口,请参阅http://docs.microsofttranslator.com/text-translate.html#!/default/get_Translate。