Florian
Florian

Reputation: 33

Calendar API Response with '403: Daily Limit Exceeded' - Google Chrome Extension

I started with an little application for showing some relevant information to our developer team, which is collected from different sources. Like google calendar, our project backoffice, an openarena server-logs etc.

I started with an web application, but then decided to switch the project to an chrome extension. Now i already had the google calendar integration up and running using the V3 Javascript(alpha) Client-Lib. This wasn't working anymore because i had to change the OAUTH dance, from the one for web-apps, to the one for packaged/installed apps.

For this i followed the tutorial (It's my first extension.) http://code.google.com/chrome/extensions/tut_oauth.html and got the OAUTH dance working again. Now i'am trying to request my calendar-data from the google api using the signedRequest-Methode from the tutorial, but alway receive the response "Daily Limit Exceeded. Please sign up" (Api Console shows i haven't performed any request).

Maybe someone has an idea what i am doing wrong here, because i tried everthing i could think about. Thanks in advance, Florian

Code - manifest.json:

{
    "name": "MIS",
    "version": "0.1",
    "description": "Monitor Information System",
    "background_page": "background.html",

    "browser_action": {
        "default_icon": "img/mis/icon.png",
        "default_title": "Mis"
    },

    "permissions": [
    "tabs",
    "https://www.googleapis.com/",
    "https://www.google.com/"]
}

background.html:

...         
oauth = ChromeExOAuth.initBackgroundPage
({
'request_url':'https://www.google.com/accounts/OAuthGetRequestToken',
'authorize_url':'https://www.google.com/accounts/OAuthAuthorizeToken',
'access_url':'https://www.google.com/accounts/OAuthGetAccessToken',
'consumer_key': 'anonymous',
'consumer_secret': 'anonymous',
'scope': 'https://www.googleapis.com/auth/calendar',
'app_name': 'Mis'
}); 
...

main.html Methode call:

function performCalendarEventsRequest(calendarId)
{
var requestUrl = 'https://www.googleapis.com/calendar/v3/calendars/'+calendarId+'/events';
var request = {
    'method': 'GET',
    'headers': {
      'GData-Version': '3.0',
      'Content-Type': 'application/atom+xml'
    },
    'parameters': {
      'alt': 'json'
    },
    'body': 'Data to send'
  };

 oauth.sendSignedRequest(requestUrl, calendarEventsRequestCallback, request);
}

Upvotes: 3

Views: 3247

Answers (2)

Armfoot
Armfoot

Reputation: 4921

I know this is an old question but I was stuck in the same error using the PHP implementation (Beta) of the API (2016-01-06), because I initially thought the setAuthConfigFile was all it was needed:

$client = new Google_Client();
$client->setApplicationName('MyCalendarAppName');
$client->setAuthConfigFile(APPPATH.'client_secret.json'); //file downloaded from GDC:
//  https://console.developers.google.com/apis/credentials?project=YOUR-PROJECT-ID
$client->addScope(Google_Service_Calendar::CALENDAR_READONLY);
$service = new Google_Service_Calendar($client);

The error was not really due to a "daily limit" of requests but to the fact that I (owner of the Google account) hadn't explicitly given access permissions to the calendar. This is how to do it:

  1. access the URL returned by the createAuthUrl() method (which can be invoked before calling the service);
  2. a "Deny" and "Allow" form for accessing the calendars shows up – press Allow;
  3. a code is returned – copy&paste this code to the authenticate($code) method and voilá, no more 403: Daily Limit Exceeded errors.

For doing this, just use the following lines before invoking the service:

//$client->createAuthUrl();
// - invoke the method above one time only: returns a URL with the "Allow" form
//   which will give the code for authentication
$client->authenticate('YOUR_CODE_GOES_HERE');
$service = new Google_Service_Calendar($client); //invokes the Calendar service

However, this will allow a one time access to the calendar. If you try this twice, you get:

Google_Auth_Exception: Error fetching OAuth2 access token, message: 'invalid_grant: Code was already redeemed.'

Meaning that a token is needed to reaccess the calendar with the reedemed code. This token is returned by the authenticate method and can be assigned to the client through the setAccessToken method:

//get the access token you previously stored or get a new one to be stored:
$accessToken = $client->authenticate('YOUR_CODE_GOES_HERE');
//after the if-else blocks...
$client->setAccessToken($accessToken);
//refresh the token if it's expired
if ($client->isAccessTokenExpired())
  $client->refreshToken($client->getRefreshToken());

The goal is to reuse that access token repeatedly (implicitly also reusing the redeemed code) and only invoke a refreshToken when the token expires. Google's quickstart creates a specific file for storing this token (calendar-php-quickstart.json) and only uses the authenticate method when the token is not found in that file. I.e. the setAccessToken is the only Client authentication method (besides the setAuthConfigFile) that needs to be used in subsequent requests.

I only fully understood this OAuth 2.0 logic by going through these errors and since this question attracted so many people already, perhaps this may help others...

Upvotes: 0

neocotic
neocotic

Reputation: 2131

Since your consumer key and secret are both set as anonymous you are not identifying your application in any way.

You can either replace these with the Client ID and Client secret values respectively, which can be found on the API Access tab on your projects page in the API Console, or you can pass the API key (found just under the auth tokens on the same page) using an additional key parameter.

This is the same for most Google APIs.

Source: http://code.google.com/apis/calendar/v3/using.html#APIKey

Upvotes: 1

Related Questions