在API请求中隐藏引用标头

时间:2015-06-19 03:55:44

标签: javascript api google-api cross-domain referrer

我需要向Google Translate 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/Request to Google Text-To-Speech API表示

  

如果HTTP请求包含Referer,Google会返回404错误   标题而不是空字符串。

请求如下:

    $.get('https://translate.google.com/translate_tts?key='+myKey+'&ie=utf-8&tl=en&q=Hello+world',
        function (returned_data) {

如何隐藏或删除引荐来源标题,以免我被阻止?

this source说要把https://href.li/...放在前面。所以我把它改成了:

$.get('https://href.li/?https://translate.google.com/translate_tts?key='+key+'&ie=utf-8&tl=zh-CN&q=你好',

我仍被封锁。

服务器端尝试:无响应。

This blog提供了一个服务器端脚本,用于将引用者设置为空字符串。他说:

  

这会从该接入点获取信息并立即将其吐出。但在使用file_get_contents请求数据之前,Referer标头设置为空字符串。

/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(root):

<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注意:第4行的/Users/myname/Sites/app/melonJS-dev/testPHP.php中的未定义索引:tl,引用者:http://melon.localhost/

     

PHP警告:file_get_contents(https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog):无法打开流:HTTP请求失败!在第6行的/Users/myname/Sites/app/melonJS-dev/testPHP.php中找不到HTTP / 1.0 404 \ r \ n,参考文献:http://melon.localhost

tail -f access log:显示使用200传递的tl参数:

  

GET   /testPHP.php?translate_tts?tl=en&q=the%20brown%20fox%20jumped%20over%20the%20lazy%20dog   HTTP / 1.1&#34; 200 -

JSONP尝试:所以我想也许将回复对象包装在<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文字转语音尝试: 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>

遵循本教程:https://www.youtube.com/watch?v=DOtkNxmg9QY

2 个答案:

答案 0 :(得分:1)

正如你在这个小提琴中看到的那样 - http://jsfiddle.net/patridge/h4Lvp/, 您可以通过调用setRequestHeader来覆盖标头值。

但是,某些标题会被浏览器阻止过度使用。

/*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");
    });
});

答案 1 :(得分:1)

为什么您的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)

您会注意到file_get_contents请求的网址不包含tl参数。这导致返回404。您可以直接在浏览器中访问该页面:

https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog

以上返回404响应:(

https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog&tl=en

但是在我们添加了一个漂亮的新闪亮tl=en参数后,它可以工作:)。