0

I am trying to retrieve events from a public group calendar to display on a Flask based website in custom HTML style via the Google Calendar API.

I have followed the documentation's quickstart guide on how to use Python to get events from the API. I have successfully managed to do this and the results are displayed in the console.

Now, I am trying to get this to work in my Flask app, however, I am unable to proceed with the formatting of the results, as I cannot even authenticate access to the API. I am not sure whether I am doing something completely stupid or silly, but several hours of debugging and Googling have not been able to resolve the issue.

I am given the following error regardless of whether I have already made the credentials file in the same directory using the quickstart example code. Note, I am using blueprints in Flask, using a common directory structure. The code below is of part of one of my blueprints that has the client_secret file and credentials file in the same directory as the main python file of that blueprint.

...    
FileNotFoundError: [Errno 2] No such file or directory: 'client_secret.json'
...
oauth2client.clientsecrets.InvalidClientSecretsError: ('Error opening file', 'client_secret.json', 'No such file or directory', 2)

Code:

@event_site.route('/events')
def events():
    # Setup the Calendar API
    SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
    store = file.Storage('credentials.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('calendar', 'v3', http=creds.authorize(Http()))

    # Call the Calendar API
    now = datetime.datetime.utcnow().isoformat() + 'Z'  # 'Z' indicates UTC time
    print('Getting the upcoming 10 events')
    events_result = service.events().list(calendarId='id@group.calendar.google.com',
                                          timeMin=now,
                                          maxResults=10, singleEvents=True,
                                          orderBy='startTime').execute()
    events = events_result.get('items', [])

    if not events:
        print('No upcoming events found.')
    for event in events:
        start = event['start'].get('dateTime', event['start'].get('date'))
        print(start, event['summary'])

    return render_template('cadet_events.html')

Thanks in advance.

Thomas Smyth
  • 512
  • 3
  • 9
  • 36
  • `client_secret.json` is in the same directory, but what matters is the current working directory. Try to print it inside the function to check (`os.getcwd()`). You can also try to specify the whole fullpath to the file – PRMoureu Jun 16 '18 at 18:58
  • 1
    `'client_secret.json'` is a *relative path*. Relative paths are resolved against the current working directory, and evidently the current working directory for your process is not the same as the location of this module. Don't rely on relative paths, always use absolute paths. `__file__` in the module is an absolute path to the module, use that to construct an absolute path to your file. – Martijn Pieters Jun 16 '18 at 19:01
  • Thanks both, that worked like a charm. – Thomas Smyth Jun 16 '18 at 19:09

0 Answers0