Jordan Reddick Jordan Reddick - 9 days ago 8
PHP Question

Google API dailyLimitExceededUnreg

I'm using the Google URL shortener API in a PHP app. It has been working for months but now I am getting this error:

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


I'm using the server key from the developer console, I regenerated the key and even deleted the server access key section and re-added it but I keep getting the same auth error.

$query_array = json_encode( array( "longUrl" => $data['long_url'], 'key' => 'AIza-Key' ) );

$curl = curl_init( 'https://www.googleapis.com/urlshortener/v1/url' );

curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' ) );

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query( $query_array ) ) ;

curl_setopt($curl, CURLINFO_HEADER_OUT, true);

$short_url = json_decode( curl_exec( $curl ) );

$header = curl_getinfo($curl, CURLINFO_HEADER_OUT );

curl_close( $curl );

print_r( $short_url ) ; // shows above error


This API shouldn't be that hard, just add the server key in the URL string, but I'm not seeing the issue. Any ideas?

Answer

You are adding the key to the POST body instead of as a URL query string. Change the code to this:

$query_array = json_encode( array( "longUrl" => $data['long_url'] ) ); 

$curl = curl_init( 'https://www.googleapis.com/urlshortener/v1/url?key=AIza-Key' );