Google Service Exception

Hi Experts,

I am trying to use the Google Calendar API with the Following code

    require_once __DIR__ . '/vendor/autoload.php';
    $client = new Google_Client();
    $client->setApplicationName('Google Calendar API PHP Quickstart');
    $client->setAuthConfig(__DIR__ . '/client_secret.json');
    $calendar = new Google_Service_Calendar($client);
    $events = $calendar->events->listEvents('');
    print_variable($events, 'Google Calendar Events');
function print_variable($var, $label, $raw = true){

    echo '<br>=========START=========<br>';
    echo '<b>' . $label . '</b>';

    if ($raw == true) echo '<pre>';


    if ($raw == true) echo '</pre>';
    echo '<br>=========END=========<br>';

Open in new window

But I getting the following error:
PHP Fatal error:  Uncaught Google_Service_Exception: {
 "error": {
  "errors": [
    "domain": "usageLimits",
    "reason": "dailyLimitExceededUnreg",
    "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
    "extendedHelp": ""
  "code": 403,
  "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
 in C:\inetpub\wwwroot\test\gmail_calendar\vendor\google\apiclient\src\Google\Http\REST.php:118
Stack trace:
#0 C:\inetpub\wwwroot\test\gmail_calendar\vendor\google\apiclient\src\Google\Http\REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#1 C:\inetpub\wwwroot\test\gmail_calendar\vendor\google\apiclient\src\Google\Task\Runner.php(181): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#2 C:\inetpub\wwwroot\test\gmail_calendar\vendor\google\apiclient\src\ in C:\inetpub\wwwroot\test\gmail_calendar\vendor\google\apiclient\src\Google\Http\REST.php on line 118

Open in new window

My first thoughts was to go under my Google API console and under Domain Verification Add a domain. This referred me to Webmaster Central, but I cannot add my localhost. Do I need to use a real domain only, and can I use a subdomain? However, maybe I am  completely on the wrong track?

Any help will be appreciated.
APD TorontoAsked:
Who is Participating?
Chris StanyonConnect With a Mentor Commented:
OK. Pretty sure in that case, you need to set up a Service Account.

Log in to your Google API Console, select Credentials and create a new Service Account Key. Give it a name and a role (probably Project / Editor will do). Make sure JSON is selected and click on Create. Save the JSON file to your app folder (ideally, accessible by your web app but not publicly accessible!).

Once you've created the Service Account, you'll see a Service Account ID - it looks like an email address. Basically, you'll need to login to your Google Calendar and share it with that email address. When sharing your calendar, make sure you select the correct permissions.

Once that's done, you can access the calendar by Authenticating with the JSON file you downloaded. Something like this:

ini_set('display_errors', 1);

require_once __DIR__ . '/vendor/autoload.php';

$client = new Google_Client();
$client->setApplicationName('My Google Calendar');
$client->setAuthConfig('service_key.json'); // the JSON file you downloaded - KEEP IT SAFE!

$service = new Google_Service_Calendar($client);

$params = array(
  'maxResults' => 5,
  'orderBy' => 'startTime',
  'singleEvents' => TRUE,
  'timeMin' => date('c'),

$results = $service->events->listEvents('', $params);

Open in new window

This gets you the results, and you can then loop through them as needed:

<?php if (count($results->getItems()) == 0): ?>

    <h2>No upcoming events found.</h2>

<?php else: ?>

    <h2>Upcoming events:</h2>

    <?php foreach ($results->getItems() as $event): ?>
            $start = $event->start->dateTime;
            if (empty($start)) {
                $start = $event->start->date;

        <p><?php printf("%s (%s)\n", $event->getSummary(), $start) ?></p>
    <?php endforeach; ?>

<?php endif; ?>

Open in new window

NerdsOfTechTechnology ScientistCommented:
It means that you need a Google Calendar API license to use the service because you exceeded the daily limit of API access -OR- you haven't added the Calander API to your account yet?

Check your Google Dashboard:

If you already have the account setup for Calander API, you might need a paid license:

So that must mean the API key exceeded the 1M access limit
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
You just need to go through the signup process + use a valid license key for access + then stay within your access limits.

The message above is clear...

"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."

Your App is exceeding the daily usage limit for anonymous version. You have to signup for the number of calls you're making to work correctly.
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

APD TorontoAuthor Commented:
The quota is 1M/dday, today I made zero.

Also, why is it  saying

"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."

.When on line 6 I (think) am using the .json file that I downloaded from the console?
APD TorontoAuthor Commented:
How do I

You just need to go through the signup process + use a valid license key for access + then stay within your access limits.

As stated I am using the provided .json file.
NerdsOfTechTechnology ScientistCommented:
The 1M quota is for authenticated users, +1M for authenticated paid users, unauthenticated users have no access (quota of zero) to Google APIs (including Calander).


To run this quickstart, you'll need:

PHP 5.4 or greater with the command-line interface (CLI) and JSON extension installed.
The Composer dependency management tool.
Access to the internet and a web browser.
A Google account with Google Calendar enabled.
Chris StanyonCommented:
Nowhere in your code are you actually handling any authentication. The client_secret.json file simply specifies the details of your App, along with the callback used AFTER a user has authenticated themselves.

The Google API uses oAuth for it's authentication, and you have a couple of options here, depending on what you want your app to do.

If you want your app (webpage) to show your own Events, then you would need to Authenticate with a Service Account. If you want your app to show your user's Events when they visit your page, then you would need to Authenticate with a Client ID (and a callback script).

Let us know exactly what you're trying to do and we'll be able to point you in the right direction.
APD TorontoAuthor Commented:
This is a web-based app for internal use by staff.  The Google Calendar portion will be for staff to schedule future appointments.

So, in this, its working with my own calendar events.
APD TorontoAuthor Commented:
Thank You!!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.