Php Google URLShortener API返回IPRefererBlock

Php Google URLShortener API返回IPRefererBlock,php,google-api,Php,Google Api,我正在尝试将Google URL shortener API与PHP结合使用: $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.g

我正在尝试将Google URL shortener API与PHP结合使用:

$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控制台注册了所有的请求,但遗憾的是,注册为“客户端错误”,而不是“成功”
每当用户访问页面时,就会触发脚本


我真的很感激任何能帮助我克服这个烦人问题的建议。我似乎找不到任何“解决方案”。

这是应用程序中的应用程序设置问题。在凭证屏幕上,它列出了允许使用API密钥的IP。这是一种安全预防措施,有助于防止钥匙泄漏

您需要将允许的IP编辑为空(不太安全),或将其更新为包含服务器的实际IP


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

谢谢您的评论。是的,我知道。问题是,我已经注册了发出调用的IP,但仍然会收到此错误。我已经注册了服务器的IP地址,但问题是,当用户访问该页面,该页面向Google API发出请求时,是服务器的IP发出请求(我打赌这是一个),还是用户的IP?我不明白API为什么不接受提供的IP号码。是否有一个日志显示试图发出请求的IP号?您使用的是服务器端的PHP,所以它是服务器IP。这可能是有史以来最尴尬的时刻之一。我已经注册了MySql服务器的IP号码,而不是我们的Web服务器。抱歉占用您的时间,谢谢您的帮助。没问题。这些错误发生在我们最好的人身上。我在OVH的服务器上也遇到了同样的问题