Google URLShortener API returns ipRefererBlocked

Google URLShortener API returns ipRefererBlocked

我正在尝试将 Google URL 缩短器 API 与 PHP:

一起使用
$apiKey = 'ABC';
$url = 'http://www.whosebug.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."
}

我检查了以下内容:

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

我非常感谢任何帮助我解决这个恼人问题的提示。我似乎没有找到任何解决方案 "out there".

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

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

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