在 API 请求中隐藏推荐人 header
Hide referrer header in API request
我需要向 Google 提出翻译 Text-To-Speech API 的请求。我有一个已启用的密钥,但一直被 No Access-Control-Allow-Origin.
阻止
我在这里发布了更多相关信息:
Google Translate API - No Access Control Origin with Text to Speech
以下来源,http://weston.ruter.net/2009/12/12/google-tts/ and Request to Google Text-To-Speech API 说
Google returns a 404 error if the HTTP request contains a Referer
header other than an empty string.
给出这样的请求:
$.get('https://translate.google.com/translate_tts?key='+myKey+'&ie=utf-8&tl=en&q=Hello+world',
function (returned_data) {
如何隐藏或删除推荐人 header 以免被屏蔽?
this source表示把https://href.li/...
放在前面。所以我将其更改为:
$.get('https://href.li/?https://translate.google.com/translate_tts?key='+key+'&ie=utf-8&tl=zh-CN&q=你好',
我仍然被封锁。
Server-Side 尝试: 无响应。
This blog 提供了一个 server-side 脚本,可将引荐来源网址设置为空字符串。他说:
This gets the information from that access point and spits it out right away. But before it requests the data using file_get_contents, the Referer header is set to an empty string.
/php/testPHP.php:
$qs = http_build_query(array("ie" => "utf-8","tl" => $_GET["tl"], "q" => $_GET["q"]));
$ctx = stream_context_create(array("http"=>array("method"=>"GET","header"=>"Referer: \r\n")));
$soundfile = file_get_contents("http://translate.google.com/translate_tts?".$qs, false, $ctx);
header("Content-type: audio/mpeg");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
echo($soundfile);
index.php(根):
<audio controls="controls" autoplay="autoplay" style="display:none;">
<source src="/php/testPHP.php?translate_tts?tl=en&q=the%20brown%20fox%20jumped%20over%20the%20lazy%20dog." type="audio/mpeg" />
</audio>
tail -f apache 错误日志:
PHP Notice: Undefined index: tl in /Users/myname/Sites/app/melonJS-dev/testPHP.php on line 4, referer: http://melon.localhost/
PHP Warning: file_get_contents(https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found\r\n in /Users/myname/Sites/app/melonJS-dev/testPHP.php on line 6, referer: http://melon.localhost
tail -f access log: 显示 tl
参数以 200 传入:
GET
/testPHP.php?translate_tts?tl=en&q=the%20brown%20fox%20jumped%20over%20the%20lazy%20dog
HTTP/1.1" 200 -
JSONP 尝试: 所以我认为也许将 return object 包装在 <script>
中可以解决问题。我没有收到任何回复。
$.ajax({
url: 'https://translate.google.com/translate_tts?key='+key+'&ie=utf-8&tl=zh-CN&q=你好',
type: 'GET',
dataType: 'jsonp',
error: function(xhr, status, error) {
alert("error");
},
success: function(json) {
alert("success");
}
});
HTML/JS text-to-speech 尝试: 404 阻止
js:
<script>
$(function() {
$('a.say').on('click', function(e) {
e.preventDefault();
var text = $('input[name="text"]').val();
text = encodeURIComponent(text);
console.log(text);
//var url = 'https://translate.google.com/translate_tts?&ie=utf-8&tl=zh-CN&q=' + text;
var url = 'https://translate.google.com/translate_tts?ie=UTF-8&q=' + text + '&tl=en';
$('audio').attr('src', url).get(0).play();
});
});
</script>
html:
<input type="text" name="text">
<a href="#" class="say">Say it</a>
<audio src="" class="speech" hidden></audio>
正如您在此 fiddle - http://jsfiddle.net/patridge/h4Lvp/ 中所见,
您可以通过调用 setRequestHeader
覆盖 header 值。
但是,一些 header 被浏览器阻止以被覆盖。
/*global jQuery*/
(function ($) {
$.ajaxSetup({
"beforeSend": function(xhr) {
// Works fine.
xhr.setRequestHeader("X-Requested-With", {
toString: function() {
return "";
}
});
// Logs error on Chrome (probably others) as "Refused to set unsafe header "Referer".
xhr.setRequestHeader("Referer", {
toString: function() {
return "";
}
});
}
});
}(jQuery));
jQuery(function () {
$.ajax({
url: "http://fiddle.jshell.net/",
dataType: "html"
}).done(function (data) {
$("<div>").text("ajax success").appendTo("body");
});
});
为什么您的 PHP 脚本失败:
如果您查看来自 PHP 的错误响应:
PHP Warning: file_get_contents(https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog)
您会注意到 url file_get_contents
请求不包含 tl
参数。这导致返回 404。您可以直接在浏览器中访问该页面:
https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog
上面returns一个404响应:(
https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog&tl=en
但是在我们添加一个漂亮的新闪亮 tl=en
参数后,它起作用了:)。
我需要向 Google 提出翻译 Text-To-Speech API 的请求。我有一个已启用的密钥,但一直被 No Access-Control-Allow-Origin.
阻止我在这里发布了更多相关信息:
Google Translate API - No Access Control Origin with Text to Speech
以下来源,http://weston.ruter.net/2009/12/12/google-tts/ and Request to Google Text-To-Speech API 说
Google returns a 404 error if the HTTP request contains a Referer header other than an empty string.
给出这样的请求:
$.get('https://translate.google.com/translate_tts?key='+myKey+'&ie=utf-8&tl=en&q=Hello+world',
function (returned_data) {
如何隐藏或删除推荐人 header 以免被屏蔽?
this source表示把https://href.li/...
放在前面。所以我将其更改为:
$.get('https://href.li/?https://translate.google.com/translate_tts?key='+key+'&ie=utf-8&tl=zh-CN&q=你好',
我仍然被封锁。
Server-Side 尝试: 无响应。
This blog 提供了一个 server-side 脚本,可将引荐来源网址设置为空字符串。他说:
This gets the information from that access point and spits it out right away. But before it requests the data using file_get_contents, the Referer header is set to an empty string.
/php/testPHP.php:
$qs = http_build_query(array("ie" => "utf-8","tl" => $_GET["tl"], "q" => $_GET["q"]));
$ctx = stream_context_create(array("http"=>array("method"=>"GET","header"=>"Referer: \r\n")));
$soundfile = file_get_contents("http://translate.google.com/translate_tts?".$qs, false, $ctx);
header("Content-type: audio/mpeg");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
echo($soundfile);
index.php(根):
<audio controls="controls" autoplay="autoplay" style="display:none;">
<source src="/php/testPHP.php?translate_tts?tl=en&q=the%20brown%20fox%20jumped%20over%20the%20lazy%20dog." type="audio/mpeg" />
</audio>
tail -f apache 错误日志:
PHP Notice: Undefined index: tl in /Users/myname/Sites/app/melonJS-dev/testPHP.php on line 4, referer: http://melon.localhost/
PHP Warning: file_get_contents(https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found\r\n in /Users/myname/Sites/app/melonJS-dev/testPHP.php on line 6, referer: http://melon.localhost
tail -f access log: 显示 tl
参数以 200 传入:
GET /testPHP.php?translate_tts?tl=en&q=the%20brown%20fox%20jumped%20over%20the%20lazy%20dog HTTP/1.1" 200 -
JSONP 尝试: 所以我认为也许将 return object 包装在 <script>
中可以解决问题。我没有收到任何回复。
$.ajax({
url: 'https://translate.google.com/translate_tts?key='+key+'&ie=utf-8&tl=zh-CN&q=你好',
type: 'GET',
dataType: 'jsonp',
error: function(xhr, status, error) {
alert("error");
},
success: function(json) {
alert("success");
}
});
HTML/JS text-to-speech 尝试: 404 阻止
js:
<script>
$(function() {
$('a.say').on('click', function(e) {
e.preventDefault();
var text = $('input[name="text"]').val();
text = encodeURIComponent(text);
console.log(text);
//var url = 'https://translate.google.com/translate_tts?&ie=utf-8&tl=zh-CN&q=' + text;
var url = 'https://translate.google.com/translate_tts?ie=UTF-8&q=' + text + '&tl=en';
$('audio').attr('src', url).get(0).play();
});
});
</script>
html:
<input type="text" name="text">
<a href="#" class="say">Say it</a>
<audio src="" class="speech" hidden></audio>
正如您在此 fiddle - http://jsfiddle.net/patridge/h4Lvp/ 中所见,
您可以通过调用 setRequestHeader
覆盖 header 值。
但是,一些 header 被浏览器阻止以被覆盖。
/*global jQuery*/
(function ($) {
$.ajaxSetup({
"beforeSend": function(xhr) {
// Works fine.
xhr.setRequestHeader("X-Requested-With", {
toString: function() {
return "";
}
});
// Logs error on Chrome (probably others) as "Refused to set unsafe header "Referer".
xhr.setRequestHeader("Referer", {
toString: function() {
return "";
}
});
}
});
}(jQuery));
jQuery(function () {
$.ajax({
url: "http://fiddle.jshell.net/",
dataType: "html"
}).done(function (data) {
$("<div>").text("ajax success").appendTo("body");
});
});
为什么您的 PHP 脚本失败:
如果您查看来自 PHP 的错误响应:
PHP Warning: file_get_contents(https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog)
您会注意到 url file_get_contents
请求不包含 tl
参数。这导致返回 404。您可以直接在浏览器中访问该页面:
https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog
上面returns一个404响应:(
https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog&tl=en
但是在我们添加一个漂亮的新闪亮 tl=en
参数后,它起作用了:)。