gpt4 book ai didi

php - Google URLShortener API 返回 ipRefererBlocked

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:59:02 24 4
gpt4 key购买 nike

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

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

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

最佳答案

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

enter image description here

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

enter image description here

关于php - Google URLShortener API 返回 ipRefererBlocked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29123516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com