【问题标题】:Google URLShortener API returns ipRefererBlockedGoogle URLShortener API 返回 ipRefererBlocked
【发布时间】:2015-03-18 13:34:29
【问题描述】:

我正在尝试将 Google URL Shorter 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 控制台注册了所有请求,但遗憾的是作为“客户端错误”而不是“成功”

每当用户访问该页面时都会触发该脚本。

我非常感谢任何可以帮助我解决这个烦人问题的提示。我似乎没有在“外面”找到任何解决方案。

【问题讨论】:

  • 感谢您的评论。是的,我知道。问题是我已经注册了拨打电话的 IP,但仍然收到此错误。

标签: php google-api


【解决方案1】:

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

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

【讨论】:

  • 我已经注册了服务器的IP地址,但问题是,当用户访问该页面并且该页面向Google API发出请求时,是服务器的IP发出请求吗(我敢打赌在这个)还是用户的?我不明白为什么 API 不接受提供的 IP 号码。是否有日志显示尝试发出请求的 IP 号码?
  • 您使用的是服务器端的 PHP,所以它是服务器 IP。
  • 嗯,这可能是有史以来最尴尬的时刻之一。我已经注册了我们的 MySql 服务器的 IP 号码,而不是我们的网络服务器。很抱歉占用您的时间,感谢您的帮助。
  • 没问题。这些错误发生在我们最优秀的人身上。
【解决方案2】:

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

【讨论】:

  • 我在 OVH 的服务器上遇到了同样的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-05
  • 2016-11-05
  • 2016-04-09
  • 1970-01-01
  • 2015-05-28
  • 2018-10-15
  • 2016-06-10
相关资源
最近更新 更多