phpgoogle-apigoogle-url-shortener

Google Shortener API limitation


We are using Google Shortener in our projects, it seems Google has two limitations :

At the 11:00 AM we have a cron job which runs a code and needs to short about 15,000 URLs,

Yesterday we used just one key, but today we add 6 keys to bypass this limitation and each minute use one of them with the following code

    $googleAPIKeys = [
        'KEY1CODE',
        'KEY2CODE',
        'KEY3CODE',
        'KEY4CODE',
        'KEY5CODE',
        'KEY6CODE'
    ];
    $roundRobinKey = date('i') % count($googleAPIKeys);
    try {
        $req = $client->post('https://www.googleapis.com/urlshortener/v1/url?key=' . $googleAPIKeys[$roundRobinKey], [
            'json' => [
                'longUrl' => $url
            ]
        ]);
    } catch (ClientException $e) {
        return $url;
    }

    $result = json_decode($req->getBody()->getContents());

But still faced with 403 (limitations exceed) enter image description here

enter image description here

it seems strange, Why This happening? What is the true limitation of shortener google service? how can I bypass this limitation?

Another note: in the single key scenario if you limitation exceed ( 100 per 100 seconds) if you try to use that key again after 5 min still faced with 403 limitations exceed problem !!! it seems google limitations is not as they said !!!

Update

After reading Standard Query Parameters and Capping API usage I'd changed my request to the following

  $req = $client->post('https://www.googleapis.com/urlshortener/v1/url?quotaUser=' . $userID . '&key=' . $myKey, [
    'json' => [
        'longUrl' => $url
    ]
]);

Which userID is unique userID per request ( because we generate each url per user) . but still facing with 403 !!!

enter image description here

Any Idea?


Solution

  • The limit is going to be project and user based. however it can also be ip based.

    work arounds

    1. create multiple projects on Google developers console. each with one API key.
    2. send the optional parm userid with each request useing a random variable.
    3. check the quota tab in developers console see if you can increase the quota it will have a pencil icon.

    Note the amount of time you are locked out should be around an hour.

    quotaUser Alternative to userIp. Lets you enforce per-user quotas from a server-side application even in cases when the user's IP address is unknown. This can occur, for example, with applications that run cron jobs on App Engine on a user's behalf. You can choose any arbitrary string that uniquely identifies a user, but it is limited to 40 characters. Overrides userIp if both are provided. Learn more about Capping API usage.

    You add it as an additional parm in your request

    quotaUser=xxxx