在API请求隐藏网址标头 [英] Hide referrer header in API request

查看:456
本文介绍了在API请求隐藏网址标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要请求谷歌翻译文本到语音的API。我有一个启用键,但不断收到阻止任何访问控制允许来源。

我已经张贴更多关于它在这里:

<一个href=\"http://stackoverflow.com/questions/30929518/google-translate-api-no-access-control-origin-with-text-to-speech\">Google翻译API - 没有访问控制源流文本到语音

以下来源, http://weston.ruter.net/2009 / 12/12 /谷歌TTS / 请求谷歌文本-to-语音API


  

谷歌如果HTTP请求包含的Referer返回404错误
  不是空字符串头其他。


给定一个类似的请求:

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

我如何隐藏或删除网址标头,所以我不会被阻止?

这个来源说把 HTTPS://href.li / ... 在前面。所以,我把它改为:

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

和我仍然受阻。


服务器端的尝试:无响应

此博客提供服务器端脚本,设置引荐为空字符串。他说:


  

这会从该接入点的信息,并吐出它的时候了。但它使用的file_get_contents请求数据前,Referer头被设置为空字符串。


/php/testPHP.php:

  $ QS = http_build_query(阵列(IE=&gt;中UTF-8,TL=&GT; $ _GET [TL],Q=&GT ; $ _GET [q));
$ CTX = stream_context_create(阵列(HTTP=&GT;阵列(办法=&gt;中GET,头=&gt;中的Referer:\\ r \\ n)));
$音效档=的file_get_contents(http://translate.google.com/translate_tts?$ QS,假,$ CTX);标题(内容类型:音频/ MPEG);
标题(内容传输编码:二进制);
头('杂注:无缓存');
头('过期:0');回波($音效档);

的index.php(根):

 &LT;音响控制=控制自动播放=自动播放的风格=显示:无;&GT;
            &lt;信源src=\"/php/testPHP.php?translate_tts?tl=en&q=the%20brown%20fox%20jumped%20over%20the%20lazy%20dog.\"键入=音频/ MPEG/&GT;
&LT; /音频&GT;

尾-f Apache的错误日志:


  

PHP公告:未定义指数:TL在/Users/myname/Sites/app/melonJS-dev/testPHP.php上线4条,引用者:的 HTTP://melon.localhost/


  
  

PHP的警告:的file_get_contents(<一个href=\"https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog\" rel=\"nofollow\">https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog):未能打开流:HTTP请求失败HTTP / 1.0 404未找​​到\\ r \\ n的/Users/myname/Sites/app/melonJS-dev/testPHP.php第6行,引用者:的 HTTP://melon.localhost


尾-f访问日志:的显示¥参数获得通过在具有200:


  

GET
  /testPHP.php?translate_tts?tl=en&q=the%20brown%20fox%20jumped%20over%20the%20lazy%20dog
  HTTP / 1.1200 -



JSONP尝试:所以,我想也许是包裹在&LT返回的对象;脚本&GT; 将解决这一问题。我没有得到任何回应。

  $阿贾克斯({
        网址:'https://translate.google.com/translate_tts?key='+key+'&ie=utf-8&tl=zh-CN&q=你好',
            输入:GET,
            数据类型:'JSONP',
            错误:功能(XHR,状态,错误){
            警报(错误);
        },
        成功:函数(JSON){
            警报(成功);
        }
        });


HTML / JS文本到语音的尝试: 404块

JS:

 &LT;脚本&GT;
        $(函数(){
            $('a.say')。在('点击',功能(E){
                亦即preventDefault();
                变种文字= $('输入[名称=TEXT])VAL()。
                文= EN codeURIComponent(文本);
                的console.log(文本);
                // VAR URL ='https://translate.google.com/translate_tts?&ie=utf-8&tl=zh-CN&q='+文字;
                VAR URL ='https://translate.google.com/translate_tts?ie=UTF-8&q='+文字+'和; TL = EN';
                $('声音')ATTR('src'中,URL)获得(0).play();
            });
        });
    &LT; / SCRIPT&GT;

HTML

 &LT;输入类型=文本名称=TEXT&GT;
    &LT; A HREF =#类=说&GT;说它&LT; / A&GT;    &LT;音频SRC =级=讲话隐藏&GT;&LT; /音频&GT;

在此之后教程: https://www.youtube.com/watch?v=DOtkNxmg9QY


解决方案

为什么你的PHP脚本失败:

如果你看一看从PHP的错误响应:

  PHP的警告: file_get_contents(https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog)

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

<一个href=\"https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog\" rel=\"nofollow\">https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog

以上返回一个404响应:(

<一个href=\"https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog&tl=en\" rel=\"nofollow\">https://translate.google.com/translate_tts?ie=utf-8&q=the+brown+fox+jumped+over+the+lazy+dog&tl=en

但经过我们添加了一个不错的新闪亮 TL = EN 参数,它的工作原理。)

I need to make requests to Google Translate Text-To-Speech API. I have an enabled key but keep getting blocked by No Access-Control-Allow-Origin.

I've posted more about it here:

Google Translate API - No Access Control Origin with Text to Speech

The following sources, http://weston.ruter.net/2009/12/12/google-tts/ and Request to Google Text-To-Speech API say that

Google returns a 404 error if the HTTP request contains a Referer header other than an empty string.

Given a request like:

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

How do I hide or remove the referrer header so I'm not blocked?

this source says to put https://href.li/... in front. So I changed it to:

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

And am still blocked.


Server-Side attempt: No response.

This blog provides a server-side script which sets the referrer to an empty string. He says:

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 (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 error logs:

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: Shows the tl param getting passed in with a 200:

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


JSONP Attempt: so I thought perhaps wrapping the return object in a <script> would solve the issue. I get no response.

    $.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 attempt: 404 block

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>

Following this tutorial: https://www.youtube.com/watch?v=DOtkNxmg9QY

解决方案

Why your PHP script is failing:

If you take a look at your error response from PHP:

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

You'll notice that the url file_get_contents is requesting does not contain a tl parameter. This is causing a 404 to be returned. You can visit the page directly in your browser:

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

The above returns a 404 response :(

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

But after we add a nice new shiny tl=en parameter, it works :).

这篇关于在API请求隐藏网址标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆