7

For some reason my script stopped working today. When I look in the API control panel says I still have 100% left of usage. Any ideas? Did they change the auth way?

function url_small($url)
    {
        //This is the URL you want to shorten
        $longUrl = $url;
        $apiKey = '#####HIDDEN######';
        //Get API key from : http://code.google.com/apis/console/

        $postData = array('longUrl' => $longUrl, '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);
        curl_close($curlObj);

        return $json->id;
    }

Response

stdClass Object
(
    [error] => stdClass Object
        (
            [errors] => Array
                (
                    [0] => stdClass Object
                        (
                            [domain] => usageLimits
                            [reason] => dailyLimitExceededUnreg
                            [message] => Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.
                            [extendedHelp] => https://code.google.com/apis/console
                        )

                )

            [code] => 403
            [message] => Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.
        )

)
Richard Dev
  • 1,110
  • 7
  • 21
  • 1
    Says right there in the error message: **Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.** I guess you need to sign up, eh? – Robert Harvey Mar 23 '15 at 15:26
  • @RobertHarvey yeah that is what is confusing me. It is passing the api key which should be the authentication, and the api use for today is 0. – Richard Dev Mar 23 '15 at 15:53
  • Check [here](https://www.google.com/search?q=daily%20limit%20for%20unauthenticated%20use%20exceeded.%20continued%20use%20requires%20signup) and report back. – Robert Harvey Mar 23 '15 at 15:54
  • @RobertHarvey thanks for the google search link. I looked through that before I posted. Not sure why you put this post on hold for off-topic. Again this was a functioning script and has stopped overnight so I was wondering if anyone else is seeing this issue. – Richard Dev Mar 23 '15 at 16:33
  • How will that help you? Yes, someone else has almost certainly seen it; it's a big world out there. You might want to check with Google to see if your key is still good. – Robert Harvey Mar 23 '15 at 16:34
  • 1
    @RobertHarvey How will that help? Really? Doesn't matter. I found what changes they require and got it working. Have a good one. I would add the fix if the question wasn't on hold. – Richard Dev Mar 23 '15 at 16:37
  • Question reopened. Go for it. – Robert Harvey Mar 23 '15 at 16:38
  • 1
    Rob - this post was extremely useful for me as I've had the same problem. I'm glad Richard pushed to discover and post the fix no thanks to your pedantic approach. Good for you Richard. Thanks very much. – dai.hop Mar 26 '15 at 12:29

1 Answers1

27

My below answer is no longer valid. Google now makes you use Firebase for URL Shortening. Easy to setup.

https://firebase.google.com/docs/dynamic-links/rest


So it turns out this old function that is displayed in multiple websites now needs the api key to be displayed in the URL section too for google to register the request to your account.

curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');

switched to this

curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?key='.$apiKey);
Richard Dev
  • 1,110
  • 7
  • 21