谷歌翻译TTS API被阻止

gin*_*ang 21 google-text-to-speech

Google实施了验证码以阻止用户访问TTS翻译API https://translate.google.com/translate_tts?ie=UTF-8&q=test&tl=zh-TW.我在我的移动应用程序中使用它.现在,它没有返回任何东西.我如何绕过验证码?

gin*_*ang 23

将限定符'&client = tw-ob'添加到查询的末尾. https://translate.google.com/translate_tts?ie=UTF-8&q=test&tl=zh-TW&client=tw-ob

这个答案不再一致.如果您滥用此权限,您的IP地址将被谷歌暂时阻止.


Guy*_*tem 12

主要有三个问题:

  1. 你必须在查询字符串中包含"client"(client = t似乎可以工作).
  2. (如果您尝试使用AJAX检索它),HTTP请求的Referer必须是https://translate.google.com/
  3. 每个查询的"tk"字段都会更改,并且必须使用匹配的哈希填充:tk = hash(q,TKK),其中q是要TTS的文本,并且当您加载翻译时TKK是全局范围中的var .google.com :(在控制台中输入'window.TKK').请参阅此回复底部的哈希函数(calcHash).

总结一下:

function generateGoogleTTSLink(q, tl, tkk) {
    var tk = calcHash(q, tkk);
    return `https://translate.google.com/translate_tts?ie=UTF-8&total=1&idx=0&client=t&ttsspeed=1&tl=${tl}&tk=${tk}&q=${q}&textlen=${q.length}`;
}

generateGoogleTTSLink('ciao', 'it', '410353.1336369826');
// see definition of "calcHash" in the bottom of this comment.
Run Code Online (Sandbox Code Playgroud)

=>要开始使用TKK,您可以打开Goog​​le Translate网站,然后在开发人员工具的控制台中键入"TKK"(例如:"410353.1336369826").

请注意,TKK值每小时都会发生变化,因此,旧TKK可能会在某些时候被阻塞,并且可能需要刷新它(尽管到目前为止看起来旧密钥可以工作很长时间).

如果您希望定期刷新TKK,它可以很容易地自动化,但如果您从浏览器运行代码则不行.

你可以在这里找到一个完整的NodeJS实现:https: //github.com/guyrotem/google-translate-server.它公开了一个最小的TTS API(查询,语言),并部署到一个免费的Heroku服务器,所以你可以在线测试它,如果你愿意.

function shiftLeftOrRightThenSumOrXor(num, opArray) {
	return opArray.reduce((acc, opString) => {
		var op1 = opString[1];	//	'+' | '-' ~ SUM | XOR
		var op2 = opString[0];	//	'+' | '^' ~ SLL | SRL
		var xd = opString[2];	//	[0-9a-f]

		var shiftAmount = hexCharAsNumber(xd);
		var mask = (op1 == '+') ? acc >>> shiftAmount : acc << shiftAmount;
		return (op2 == '+') ? (acc + mask & 0xffffffff) : (acc ^ mask);
	}, num);
}

function hexCharAsNumber(xd) {
	return (xd >= 'a') ? xd.charCodeAt(0) - 87 : Number(xd);
}

function transformQuery(query) {
	for (var e = [], f = 0, g = 0; g < query.length; g++) {
	  var l = query.charCodeAt(g);
	  if (l < 128) {
	  	e[f++] = l;					//	0{l[6-0]}
	  } else if (l < 2048) {
	  	e[f++] = l >> 6 | 0xC0;		//	110{l[10-6]}
	  	e[f++] = l & 0x3F | 0x80;	//	10{l[5-0]}
	  } else if (0xD800 == (l & 0xFC00) && g + 1 < query.length && 0xDC00 == (query.charCodeAt(g + 1) & 0xFC00)) {
	  	//	that's pretty rare... (avoid ovf?)
	  	l = (1 << 16) + ((l & 0x03FF) << 10) + (query.charCodeAt(++g) & 0x03FF);
	  	e[f++] = l >> 18 | 0xF0;		//	111100{l[9-8*]}
	  	e[f++] = l >> 12 & 0x3F | 0x80;	//	10{l[7*-2]}
	  	e[f++] = l & 0x3F | 0x80;		//	10{(l+1)[5-0]}
	  } else {
		e[f++] = l >> 12 | 0xE0;		//	1110{l[15-12]}
		e[f++] = l >> 6 & 0x3F | 0x80;	//	10{l[11-6]}
		e[f++] = l & 0x3F | 0x80;		//	10{l[5-0]}
	  }
	}
	return e;
}

function normalizeHash(encondindRound2) {
	if (encondindRound2 < 0) {
		encondindRound2 = (encondindRound2 & 0x7fffffff) + 0x80000000;
	}
	return encondindRound2 % 1E6;
}

function calcHash(query, windowTkk) {
	//	STEP 1: spread the the query char codes on a byte-array, 1-3 bytes per char
	var bytesArray = transformQuery(query);

	//	STEP 2: starting with TKK index, add the array from last step one-by-one, and do 2 rounds of shift+add/xor
	var d = windowTkk.split('.');
	var tkkIndex = Number(d[0]) || 0;
	var tkkKey = Number(d[1]) || 0;

	var encondingRound1 = bytesArray.reduce((acc, current) => {
		acc += current;
		return shiftLeftOrRightThenSumOrXor(acc, ['+-a', '^+6'])
	}, tkkIndex);

	//	STEP 3: apply 3 rounds of shift+add/xor and XOR with they TKK key
	var encondingRound2 = shiftLeftOrRightThenSumOrXor(encondingRound1, ['+-3', '^+b', '+-f']) ^ tkkKey;

	//	STEP 4: Normalize to 2s complement & format
	var normalizedResult = normalizeHash(encondingRound2);

	return normalizedResult.toString() + "." + (normalizedResult ^ tkkIndex)
}

// usage example:
var tk = calcHash('hola', '409837.2120040981');
console.log('tk=' + tk);
 // OUTPUT: 'tk=70528.480109'
Run Code Online (Sandbox Code Playgroud)


Arm*_*gar 5

您也可以尝试这种格式:

  1. 传递 q= 您语言的 urlencode 格式(在 JavaScript 中您可以使用encodeURI() 函数,PHP 有 rawurlencode() 函数)

  2. pass tl = 语言简称(假设 bangla = bn)

现在试试这个:

https://translate.google.com.vn/translate_tts?ie=UTF-8&q=%E0%A6%A2%E0%A6%BE%E0%A6%95%E0%A6%BE+&tl=bn&client=tw -ob