Google Calendar api with django application


I have used google calendar api with django application.

I have create some appointments on google calendar using django.

There are  following steps to integrate with django application.


1. Firstly goes to this link google developer console
     and then create a project and then create application

2.  save the client secret key and client id.

3.  Then go to your django application where you used calendar api

4. now install the library using command  <pip install google-api-python-client>then go to next step

5. I have used in my views.py

example:-
        

from googleapiclient import discovery
from oauth2client import tools
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage
import httplib

    #---------------------------------------------------------------------------
    # google_calendar_connection
    #---------------------------------------------------------------------------
    def google_calendar_connection():
        """
        This method used for connect with google calendar api.
        """
        
        flags = tools.argparser.parse_args([])
        FLOW = OAuth2WebServerFlow(
            client_id='xxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com',
            client_secret='xxxxxx',
            scope='https://www.googleapis.com/auth/calendar',
            user_agent='<application name>'
            )
        storage = Storage('calendar.dat')
        credentials = storage.get()
        if credentials is None or credentials.invalid == True:
            credentials = tools.run_flow(FLOW, storage, flags)
        
        # Create an httplib2.Http object to handle our HTTP requests and authorize it
        # with our good Credentials.
        http = httplib2.Http()
        http = credentials.authorize(http)
        service = discovery.build('calendar', 'v3', http=http)
        
        return service



and this function used in my next funtion

    def form_valid(self, form):
        """
        This method used for add event in google calendar.
        """
        
        service = self.google_calendar_connection()
        
        event = {
          'summary': "new",
          'location': "london",
          'description': "anything",
          'start': {
            'date': "2015-09-02",
          },
          'end': {
            'date': "2015-09-02",
          },
                 
        }
        
        event = service.events().insert(calendarId='primary', body=event).execute()
        
        return CreateView.form_valid(self, form) 


This will create the event on your google calendar

Hope This will helpful :)

Thanks,

Comments

  1. oauth2client is now outdated so i had used social-auth-app-django. i had implemented login with google in my djnago project and now i need to create calendar in users google account and every time.

    can i create new calendar in user's calendar.google.com via django project?

    ReplyDelete

Post a Comment

Popular posts from this blog

Create cronjob for django management command async process

Google recaptcha integration with django applicaton