我正在尝试使用带有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)
我检查过以下内容:
只要用户访问该页面,就会触发该脚本.
我非常感谢能帮助我解决这个恼人问题的任何提示.我似乎没有找到"那里"的任何解决方案.
abr*_*ham 10
这是Google Developers Console中的应用设置问题.在凭据屏幕上,它列出了允许使用API密钥的IP.这是一个安全预防措施,以帮助保护密钥泄漏.

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

| 归档时间: |
|
| 查看次数: |
12144 次 |
| 最近记录: |