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.

2

There are 2 answers

0
Rick Hellewell On BEST ANSWER

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);
3
mittra On

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?