Rate Limit on Google shortner url api

2.4k views Asked by At

Hy,

we try to short a list of links (over 20.000) using (google url shortener) but have problem with rate limit there.

At first we make sure what the rate limits are. It is: Courtesy limit: 1,000,000 requests/day (found here)

To do the calls we use Google_UrlshortenerService.php (found here: http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/urlshortener/index.php)

After first run (reached rate limit after 300 calls by having 2sec delay between each call) we ask google.de for help. We found this topic (https://groups.google.com/forum/#!topic/google-url-shortener/3t1BZuhnemM) where some guys talk about enable "Grant Access" in Extension's Options, but we do not find this option(s) in the new google cloud interface.

to make sure that we are using the oauth2 we combine our script with the auth form this one:

http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/userinfo/index.php

We see our requests listed on http://goo.gl/ but we still get rate limit errors after a little more then 300 requests.

The exact error is:

  ["domain"]=>
  string(11) "usageLimits"
  ["reason"]=>
  string(17) "rateLimitExceeded"
  ["message"]=>
  string(19) "Rate Limit Exceeded"
  ["message:protected"]=>
  string(136) "Error calling POST https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyBtn-06nR_eOOjfOPJkHtbT0oBCen6tRkQ: (403) Rate Limit Exceeded"

The code:

<?php
session_start();
set_time_limit(0);

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
require_once 'google-api-php-client/src/contrib/Google_UrlshortenerService.php';

try{
$client = new Google_Client();
$client->setApplicationName("ekomiurlshort");
$client->setDeveloperKey("our_developer_key");

$client->setClientId("our_client_id");
$client->setClientSecret("our_client_secret");
$client->setRedirectUri("out app url");

$oauth2 = new Google_Oauth2Service($client);


$service = new Google_UrlshortenerService($client);

if (isset($_REQUEST['logout'])) {
    unset($_SESSION['access_token']);
}

if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    $_SESSION['token'] = $client->getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
    return;
}

if (isset($_SESSION['token'])) {
    $client->setAccessToken($_SESSION['token']);
}

if (isset($_REQUEST['logout'])) {
    unset($_SESSION['token']);
    $client->revokeToken();
}


if ($client->getAccessToken()) {

    if (($handle = fopen("createdlinks.txt", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
             // Start to make API requests.
            $url = new Google_Url();
            $url->longUrl = $data[1];
            $short = $service->url->insert($url);

            file_put_contents('out/out.txt',$data[0].';'.implode(';',$short).PHP_EOL,FILE_APPEND);
            sleep(2);
        }
        fclose($handle);
    }

    // The access token may have been updated lazily.
    $_SESSION['token'] = $client->getAccessToken();
} else {
    $authUrl = $client->createAuthUrl();
}


}catch(Exception $e){
    ?><pre><?var_dump($e);?></pre><?
}
?>

<a class='login' href='<?php print $authUrl; ?>'>Connect Me!</a>

Can any one help or had the same problem in the past.

0

There are 0 answers