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.