Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google URLShortener API returns ipRefererBlocked

Tags:

php

google-api

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

like image 721
Shubbi Avatar asked Dec 15 '22 16:12

Shubbi


1 Answers

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.

enter image description here

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

enter image description here

like image 96
abraham Avatar answered Dec 22 '22 00:12

abraham