0

I created a reasonably simple script that would generate a random and unique password for me to reset users' passwords. Because I hate sending passwords as plain text over e-mail, my script converts the password to an image that is stored on my server. The script returns the password, the image of the password, and was supposed to shorten the URL of the password image using the Google URL-Shortener API.

Since this is something that is only used in our office and the URL of the page isn't public, I opted to use the API-Key instead of OAuth 2.0.

It was working great and then suddenly it stopped providing the shortened URL. I added in a little Error Handling code so it would display the error and it started returning this error:

403 - Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.

Here's the function I use to get the shortened URL:

function shortURL($ImgID)
{
    // This is the URL to shorten
    $ImgURL = 'http://www.mywebsite.com/temp/'.$ImgID.'.jpg';

    // Get API key from : http://code.google.com/apis/console/
    $apiKey = 'MY_API_KEY'; //browser key

    $postData = array('longUrl' => $ImgURL, 'key' => $apiKey);

    $jsonData = json_encode($postData);

    $curlObj = curl_init();

    curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
    curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curlObj, CURLOPT_HEADER, 0);
    curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
    curl_setopt($curlObj, CURLOPT_POST, 1);
    curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);

    $response = curl_exec($curlObj);

    // Change the response json string to object
    $json = json_decode($response);

    // Change the response json string to an array
    $shortLink = get_object_vars($json);

    curl_close($curlObj);

    // Error handling
    $short = objectToArray($json);
    $code = $short['error']['code'];
    $message = $short['error']['message'];

    return ($shortLink['id']?$shortLink['id']:$code.' - '.$message);

}

Like I said, it was working fine and then one day stopped. I've changed the API key with no success. I've looked at the quotas in the developers console and I'm nowhere near the limits suggested there either overall or on a per user basis.

I'm turning here because my Google-Fu is failing me and I haven't been able to find any explanation for what changed and why my previously working script has stopped.

mittra
  • 29
  • 1
  • 11

2 Answers2

2

See the answer in this post Goo.gl URL Shortener Stopped Working (php/curl)

You need to add the API key to the request

 $apikey = "YOURAPIKEY";
 curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?key='.$apikey);
Community
  • 1
  • 1
Rick Hellewell
  • 1,032
  • 11
  • 34
1

An interesting development.... I made a small change:

I changed the $postData array to only include the longURL and added the API key to the CURLOPT_URL and after refreshing the original page to pull new code, it worked.

When I go to goo.gl and sign-in with the user that "Activated" the API Key, it doesn't show the most recent ones I've created.

So while the original question seems answered (I've got it working now), I'm left with two more questions:

  1. Why did it work?
  2. Why isn't it showing me the history of created shortened URLs?
mittra
  • 29
  • 1
  • 11
  • Curiouser and more curious. When I posted the answer above yesterday, I omitted the fact that after making the change to the $postData and adding the $apiKey to my curl URL that I received a new error message: "403 - 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 referrer should be allowed." (continued in the next comment) – mittra Jun 17 '15 at 14:49
  • (continued from previous comment) When I got the message, I went back to the developers console and removed the referring website from the browser key and tried again. It still gave me the new error. So I went in and generated an API server key and listed the IP of what I believed to be our server. It still produced this new error. So, I removed the IP of the web server from the list and tried again. This time it didn't give me the error. (continued in the next comment) – mittra Jun 17 '15 at 14:51
  • (continued from the previous comment) So I went back in again and changed it back to the API browser key with our website as the referrer so I could get the error message to share here as an update and the error didn't reproduce itself again. I figured it was a weird timing issue, removed the referring website again and everything still worked (my random pw and image were generated, and a Google Short-URL showed up with no errors). That's when I posted the "Answer" to my own question. This morning, the new error is back and It's still not showing the shortened URLs in the account history. – mittra Jun 17 '15 at 14:52