Google URLShortener API返回ipRefererBlocked

时间:2015-03-18 13:34:30

标签: php google-api

我正在尝试使用带有PHP的Google URL缩短程序API:

$apiKey = 'ABC';
$url = 'http://www.stackoverflow.com/';

$postData = array('longUrl' => $url);
$jsonData = json_encode($postData);

$curlObj = curl_init();

curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?key=' . $apiKey);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8","Accept:application/json, text/javascript, */*; q=0.01"));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);

$result = curl_exec($curlObj);

但是我收到以下错误消息:

{
    "error": {
        "errors": [{
            "domain": "usageLimits",
            "reason": "ipRefererBlocked",
            "message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.",
            "extendedHelp": "https://console.developers.google.com"
      }],
    "code": 403,
    "message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed."
}

我检查了以下内容:

  • 设置了引用(http和https)
  • API控制台注册所有请求,但遗憾的是“客户端错误”而不是“成功”

只要用户访问该页面,就会触发该脚本。

我非常感谢能帮助我解决这个烦人问题的任何提示。我似乎没有找到“那里”的任何解决方案。

2 个答案:

答案 0 :(得分:10)

这是Google Developers Console中的应用设置问题。在凭据屏幕上,它列出了允许使用API​​密钥的IP。如果密钥泄漏,这是一个安全预防措施,可以帮助保护密钥。

enter image description here

您需要将允许的IP编辑为空(安全性较低)或更新它以包含服务器的实际IP。

enter image description here

答案 1 :(得分:2)

我只是补充一点,当我从Rackspace云服务器使用Google API时,我遇到了类似的问题。如果添加了任何IP地址,则它将始终被阻止(多次检查以确保它是正确的IP,包括从服务器向报告远程IP的页面发出请求)。所以从Rackspace云主机,我必须允许任何IP。如果我在其他任何地方托管,则IP限制按设计工作。