在 API 请求中隐藏引用标头 [英] Hide referrer header in API request

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

问题描述

我需要向 Google Translate Text-To-Speech API 提出请求.我有一个已启用的密钥,但一直被 No Access-Control-Allow-Origin 阻止.

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 翻译 API - 无访问控制文本到语音的来源

以下来源,http://weston.ruter.net/2009/12/12/google-tts/向谷歌请求Text-To-Speech API

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

如果 HTTP 请求包含 Referer,Google 会返回 404 错误不是空字符串的标头.

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) {

如何隐藏或删除引荐来源网址以免被阻止?

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

这个来源说把https://href.li/... 在前面.所以我把它改成:

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=你好',

我还是被屏蔽了.

服务器端尝试:无响应.

本博客提供将引用者设置为空字符串的服务器端脚本.他说:

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

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

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

PHP Notice: Undefined index: tl in /Users/myname/Sites/app/melonJS-dev/testPHP.php on line 4, referer: 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 请求失败!HTTP/1.0 404 Not Found\r\n in/Users/myname/Sites/app/melonJS-dev/testPHP.php on line 6,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 访问日志: 显示以 200 传入的 tl 参数:

tail -f access log: Shows the tl param getting passed in with a 200:

获取/testPHP.php?translate_tts?tl=en&q=the%20brown%20fox%20jumped%20over%20the%20lazy%20dogHTTP/1.1" 200 -

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

<小时>

JSONP 尝试: 所以我想也许将返回对象包装在

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