0

In a django application, I try to have RW access to a google calendar which I own myself.

Tried several ways with a service account & client secrets, but all resulting in authentication errors.

The API explorer works, but it requests consent in a popup window, which is obviously not acceptable.

Documentation on google OAuth2 describes several scenarios. Probably "web server application" applies here? It says:

"The authorization sequence begins when your application redirects a browser to a Google URL; the URL includes query parameters that indicate the type of access being requested. Google handles the user authentication, session selection, and user consent. The result is an authorization code, which the application can exchange for an access token and a refresh token."

Again, we do not want a browser redirection, we want direct access to the google calendar.

So question is: how can a django server access a google calendar, on which I have full rights, view events and add events using a simple server stored key or similar mechanism?

5

1 Answer 1

1

With help of DalmTo and this great article, I got RW access to a google calendar working from python code. I will summarize the solution here.

Here are the steps:

First of all register for a google service account: Service accounts are pre-authorized accounts that avoid you need to get consent or refresh keys every time: https://developers.google.com/identity/protocols/OAuth2ServiceAccount (The part on G-suite can be ignored)

Download the service account credentials and store them safely. Your python code will need access to this file.

Go to your google calendar you want to get access to. e.g. https://calendar.google.com/calendar/r/month On the right side you see your calendars. Create an additional one for testing (since we'll write to it soon). Then point to this new calendar: click the 3 dots next to it and edit the sharing settings. Add the service account email address to the share under "share with specific people". (you can find the service account email address in the file downloaded previously under "client_email") In the same screen, note the "calendar ID", you'll need it in below code.

Now you service account has the RW rights to the calendar.

Add at least one event to the calendar using the web UI (https://calendar.google.com/calendar/r/month) so we can read and change it from below code.

Then use following python code to read the calendar and change an event.

from google.oauth2 import service_account
import googleapiclient.discovery

SCOPES = ['https://www.googleapis.com/auth/calendar']
SERVICE_ACCOUNT_FILE = '<path to your service account file>'
CAL_ID = '<your calendar ID>'

credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = googleapiclient.discovery.build('calendar', 'v3', credentials=credentials)

events_result = service.events().list(calendarId=CAL_ID).execute()
events = events_result.get('items', [])
event_id = events[0]['id']
event = events[0]
service.events().update(calendarId=CAL_ID, eventId=event_id, body={"end":{"date":"2018-03-25"},"start":{"date":"2018-03-25"},"summary":"Kilroy was here"}).execute()

And there you go... read an event and updated the event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.