Google Calendar Integration with Django

后端 未结 3 894
不知归路
不知归路 2020-12-29 15:09

Is there a fully fledged Django-based example of a Google Calendar integration? I was reading through Google\'s example page but their link at the bottom is outdated.

<
3条回答
  •  心在旅途
    2020-12-29 15:24

    As this post was quite a while ago I wanted to share my 2020 version of it. Thanks for this post. Helped me a lot to achieve my goal.

    import datetime
    from datetime import timedelta
    
    import pytz
    from googleapiclient.discovery import build
    from oauth2client.service_account import ServiceAccountCredentials
    
    service_account_email = "INSERT_HERE"
    SCOPES = ["https://www.googleapis.com/auth/calendar"]
    credentials = ServiceAccountCredentials.from_json_keyfile_name(
        filename="FILENAME.json", scopes=SCOPES
    )
    
    
    def build_service():
        service = build("calendar", "v3", credentials=credentials)
        return service
    
    
    def create_event():
        service = build_service()
    
        start_datetime = datetime.datetime.now(tz=pytz.utc)
        event = (
            service.events()
            .insert(
                calendarId="CALENDARID@group.calendar.google.com",
                body={
                    "summary": "Foo",
                    "description": "Bar",
                    "start": {"dateTime": start_datetime.isoformat()},
                    "end": {
                        "dateTime": (start_datetime + timedelta(minutes=15)).isoformat()
                    },
                },
            )
            .execute()
        )
    
        print(event)
    
    create_event()
    

提交回复
热议问题