我正在使用客户端 Google 的翻译 API (v2),这意味着我的 API key 将对所有人公开。我正在通过 AJAX 调用 /translate 方法。
在谷歌下>凭据> API key -> key 限制->应用程序限制
我想只允许从 www.aliceandbob.com 域调用 API。
在以下位置添加此域后:HTTP 引荐来源网址(网站)。我收到 403 forbidden 错误,但是当我在我的 http 请求中手动设置 http referer header 时,它通过了并且 API 以答案响应我。
function sendPost($data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://translation.googleapis.com/language/translate/v2");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
--> curl_setopt($ch, CURLOPT_REFERER, "http://www.aliceanbob.com");
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
并像这样传递数据
$data = array('q' => '这就是世界', 'target' => 'de', 'key' => "#my_api_key#");
Responses OK 200.
但是当我删除时:
curl_setopt($ch, CURLOPT_REFERER, "http://www.aliceandbob.com");
我得到了回应:
403 Forbidden Requests from referer \u003cempty\u003e are blocked.
知道如何防止只有一个网站(域)可以正确调用 API,但不让任何其他人手动设置 http referer 和行为域(www.aliceandbob.com)吗?
最佳答案
关于http - 谷歌翻译 API v2 : Allowing certain HTTP referrers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49454315/