Google URLShortener API返回ipRefererBlocked

Shu*_*bbi 4 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);
Run Code Online (Sandbox Code Playgroud)

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

{
    "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."
}
Run Code Online (Sandbox Code Playgroud)

我检查过以下内容:

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

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

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

abr*_*ham 10

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

在此输入图像描述

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

在此输入图像描述

  • 好吧,这可能是有史以来最尴尬的时刻之一。我已经注册了我们的MySql服务器的IP号,而不是我们的Web服务器的IP号。很抱歉占用您的时间,并感谢您的帮助。 (2认同)
  • 没问题.这些错误发生在我们最好的人身上. (2认同)