This project lays out how to integrate google Calendar with your Django application using Service accounts. It should give you enough to integrate your application without problems. For more information please look it up using google or email me.
Service accounts allow you to integrate your application without needing permissions from each user and without having to make them log into their Google account.
- Run the following command to initialize the Django Project
pip install django google-api-python-client oauth2client gunicorn whitenoise
or
pipenv install
or
pip install requirements.txt
Set the values in the .env file
calendarId=...
UPDATE_GOOGLE=True
client_secret=...
- Go to the Google Cloud Platform and make and account/log in.
- Create a new Google Cloud and call it whatever you want. Im calling mine
Test Google Events
- On the top left of the screen next to the Google Cloud logo, click and select your project.
- On the home page of your project click the APIs and Services Option
- Go to the Credentials tab (on the left menu)
- Select 'Manage service accounts' (for me it was just above the Service Account table on the right)
- Select Create Service Account on the top
- fill in everything normally
- Give it the role of owner (You can give it a more defined role if you wish but it is up to you to research which role you want for that service account)
- Administrators are optional
- Once you save you will be take back to the service account page
- Save the service account email that is genereated
- Under Actions click the menu of the service account you want to use and select 'Manage Keys'
- Select 'Add Key'
- Select JSON and press 'Create'(this will download a key file to your computer)
- Open this file and put it all into one line (so that we can add it to the environment variables)
- Add it to the environment variables under the name
client_secret
- (for this to work you need to make sure that the key can be read by json.load(os.environ.get('client_secret')) otherwise it will not work correctly)
- Click on the menu on the top left and select APIs & Services
- In the 'Enabled APIs & services' screen, press 'Enable APIS and Services'
- Find and enable the 'Google Calendar API'
- Go to Google Calendar
- (Make a new Google Calendar if you dont have one already)
- On into the Settings of the Calendar
- Go to Share with specific people or groups
- Share with the service account
- 'Give make changes to events' Permisson
- Scroll down to Integrate Calendar
- Copy "Calendar ID" and add it as an environment variable called "calendarId"
- launch the project
gunicorn google_events.wsgi --log-file -
or
python manage.py runserver
- Create an Event this should now show up on the Google Calendar you linked
Thats is it!
When you open the admin interface you will see that there is a Google events page. This page shows all the events that are in the google calendar it and whether they are linked to an event in your Database.
If you set UPDATE_GOOGLE
environment variable to False, 0 events will not be sent to google.
If you feel like there is a better way of doing something, please create a merge request and i will have a look at it and merge it if it is good.