免费获取谷歌翻译api
最近做的一个项目需要调用谷歌翻译的api,来将用户输入的中文文本翻译成英文。
一开始想尝试Google Cloud 给的免费的300刀的试用,但发现还需要填写纳税人号码,visa卡号等,所以果断放弃了。
所以我接下的思路是通过程序模拟浏览器去请求https://translate.google.cn/,(如下图所示),然后获取返回的Response。
分析浏览器中的请求url的构造
可以看到浏览器请求了非常多的页面,我们需要找出Response包含翻译结果的请求页面
- 点击红框区域,ctrl+F搜索
- 搜索包含我们想得到的返回结果(chinese people)的页面,即single
- 在页面过滤器(Filter)处输入sigle,过滤出想要的页面
可以看到请求https://translate.google.cn/translate_a/single?·······,得到的Response结果正是我们想要的。
得到 Request URL(https://translate.google.cn/translate_a/single?client=webapp&sl=zh-CN&tl=en&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&source=bh&ssel=0&tsel=0&kc=1&tk=238761.343585&q=%E4%B8%AD%E5%9B%BD%E4%BA%BA) 后,需要分析Request URL的构成,然后模拟这个请求。 URL由基本请求地址:https://translate.google.cn/translate_a/single和一系列参数构成,查看 From Data,可以看到一系列的参数,重点只需要关注 tk参数,因为不同的翻译词对应的tk参数是不同的。
国外的一个大神通过js来生成的tk值的代码:
function token(a) {
var k = "";
var b = 406644;
var b1 = 3293161072;
var jd = ".";
var sb = "+-a^+6";
var Zb = "+-3^+b+-f";
for (var e = [], f = 0, g = 0; g < a.length; g++) {
var m = a.charCodeAt(g);
128 > m ? e[f++] = m: (2048 > m ? e[f++] = m >> 6 | 192 : (55296 == (m & 64512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 64512) ? (m = 65536 + ((m & 1023) << 10) + (a.charCodeAt(++g) & 1023), e[f++] = m >> 18 | 240, e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224, e[f++] = m >> 6 & 63 | 128), e[f++] = m & 63 | 128)
}
a = b;
for (f = 0; f < e.length; f++) a += e[f],
a = RL(a, sb);
a = RL(a, Zb);
a ^= b1 || 0;
0 > a && (a = (a & 2147483647) + 2147483648);
a %= 1E6;
return a.toString() + jd + (a ^ b)
};
function RL(a, b) {
var t = "a";
var Yb = "+";
for (var c = 0; c < b.length - 2; c += 3) {
var d = b.charAt(c + 2),
d = d >= t ? d.charCodeAt(0) - 87 : Number(d),
d = b.charAt(c + 1) == Yb ? a >>> d: a << d;
a = b.charAt(c) == Yb ? a + d & 4294967295 : a ^ d
}
return a
}
通过java代码调用js脚本得到tk参数值,组合参数构建url,通过httpClient模拟浏览器,请求构建好的url,得到Response。
java代码如下:
package com.jia.utils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
public class HttpC2 {
public static void main(String[] args) throws Exception {
String from = "zh-CN";//翻译前语言
String to = "en";//翻译后语言
String translateText = "中国人";//翻译文本
String baseUrl = "http://translate.google.cn/translate_a/single";
String tk = token(translateText);
String translateResult = getResponse(baseUrl,from,to,tk,translateText);
System.out.println(translateResult);
}
public static String getResponse(String baseUrl,String from,String to,String tk,String translateText)throws Exception{
Map<String, String> params = new HashMap<String, String>();
params.put("client", "webapp");
params.put("sl", from);
params.put("tl", to);
params.put("hl", "zh-CN");
params.put("dt", "at");
params.put("dt", "bd");
params.put("dt", "ex");
params.put("dt", "ld");
params.put("dt", "md");
params.put("dt", "qca");
params.put("dt", "rw");
params.put("dt", "rm");
params.put("dt", "ss");
params.put("dt", "t");
params.put("source", "bh");
params.put("ssel", "0");
params.put("tsel", "0");
params.put("kc", "1");
params.put("tk", tk);
params.put("q", translateText);
CloseableHttpClient httpClient = HttpClients.createDefault();
URIBuilder uri = new URIBuilder(baseUrl);
for (String key : params.keySet()) {
String value = params.get(key);
uri.addParameter(key, value);
}
HttpUriRequest request = new HttpGet(uri.toString());
//设置user-agent
request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
CloseableHttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
//System.out.println(result);
result = result.substring(result.indexOf("\"")+1,result.indexOf(",")-1);
//System.out.println(result);
EntityUtils.consume(entity);
response.getEntity().getContent().close();
response.close();
return result;
}
//调用js返回tk参数
private static String token(String value) {
String result = "";
ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
try {
FileReader reader = new FileReader("G:\\LAB614\\ideaproject\\test\\src\\main\\java\\com\\jia\\utils\\trans2.js");
engine.eval(reader);
if (engine instanceof Invocable) {
Invocable invoke = (Invocable)engine;
result = String.valueOf(invoke.invokeFunction("token", value));
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}