Google URLShortener API返回ipRefererBlocked [英] Google URLShortener API returns ipRefererBlocked

查看:168
本文介绍了Google URLShortener API返回ipRefererBlocked的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在PHP中使用Google URL shortener 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);

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

  {
error:{
errors:[{
domain:usageLimits,
原因:ipRefererBlocked,
message:您的API密钥上配置了per-IP或per-Referer限制,请求与这些限制不符。请使用Google Developers Console更新您的API key配置,如果来自这个IP或referer的请求应该被允许。,
extendedHelp:https://console.developers.google.com
}],
code :403,
message:您的API密钥上配置了per-IP或per-Referer限制,请求不符合这些限制。请使用Google Developers Console更新您的API密钥配置如果来自这个IP或引用者的请求应该被允许。
}

我检查了以下内容:


  • 设置了引用者(包括http和https)

  • API控制台注册所有的请求,但遗憾的是,错误而不是成功


无论用户何时访问该页面,都会触发该脚本。



我非常感谢任何能够帮助我解决这个烦人问题的技巧。我没有找到任何解决方案在那里。

解决方案

这是 Google Developers Console 。在凭证屏幕上,它列出了允许使用API​​密钥的IP。这是一个安全防范措施,以帮助保护密钥,如果它泄漏。





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


I'm trying to use the Google URL shortener API with 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);

But I'm getting the following error message:

{
    "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."
}

I've checked the following:

  • That the referers are set (both http and https)
  • That the API console registers all of the requests, but sadly as "client errors" and not as "success"

The script is triggered whenever a user visits the page.

I really appreciate any tip that helps me past this annoying problem. I don't seem to find any solution "out there".

解决方案

This is an app settings issue in the Google Developers Console. On the credentials screen it lists the IPs that are allowed to use the API Key. It's a security precaution to help protect the key if it leaks.

You need to edit allowed IPs to either be empty (less secure) or update it to include the actual IP of your server.

这篇关于Google URLShortener API返回ipRefererBlocked的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆