<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: karenapp</title>
    <description>The latest articles on DEV Community by karenapp (@karenapp).</description>
    <link>https://dev.to/karenapp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F200919%2F15dcc2ea-7d8a-49b4-b324-4523bd2eb0b2.png</url>
      <title>DEV Community: karenapp</title>
      <link>https://dev.to/karenapp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/karenapp"/>
    <language>en</language>
    <item>
      <title>How to get started with Google Calendar API using Python with Examples</title>
      <dc:creator>karenapp</dc:creator>
      <pubDate>Wed, 24 Jul 2019 14:15:20 +0000</pubDate>
      <link>https://dev.to/karenapp/how-to-get-started-with-google-calendar-api-using-python-with-examples-4c4h</link>
      <guid>https://dev.to/karenapp/how-to-get-started-with-google-calendar-api-using-python-with-examples-4c4h</guid>
      <description>&lt;p&gt;Google Calendar offers one of the most convenient and popular ways to manage schedule,&lt;br&gt;
events, meetings, or even plan out your holiday events. A few things you think will&lt;br&gt;
be straightforward end up being complicated for customised need. The Google Calendar&lt;br&gt;
API lets you automate your calendar, for those custom needs.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. How to Create a project in Google Developer Console?
&lt;/h2&gt;

&lt;p&gt;To kick things off lets head &lt;a href="https://console.developers.google.com/"&gt;Google Developers Console&lt;/a&gt; and create a new project.&lt;br&gt;
We name the project as Automating Calendar, further to it, get the credentials. &lt;/p&gt;

&lt;h2&gt;
  
  
  2. Set up the Google API Service
&lt;/h2&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar']

CREDENTIALS_FILE = 'credentials.json'

def get_calendar_service():
    creds = None
    # The file token.pickle stores the user's access and refresh 
    tokens, and is
    # created automatically when the authorization flow completes for 
      the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log 
      in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                CREDENTIALS_FILE, SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('calendar', 'v3', credentials=creds)
    return service
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  2. How to Fetch list of all Calendars from a User Account using Calendar API
&lt;/h2&gt;

&lt;p&gt;Let us create a file with name &lt;code&gt;list_calendars.py&lt;/code&gt; in the same directory as that of &lt;code&gt;cal_setup.py&lt;/code&gt; with the snippet below:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from cal_setup import get_calendar_service

def main():
   service = get_calendar_service()
   # Call the Calendar API
   print('Getting list of calendars')
  calendars_result = service.calendarList().list().execute()

   calendars = calendars_result.get('items', [])

   if not calendars:
       print('No calendars found.')
   for calendar in calendars:
       summary = calendar['summary']
       id = calendar['id']
       primary = "Primary" if calendar.get('primary') else ""
       print("%s\t%s\t%s" % (summary, id, primary))

if __name__ == '__main__':
   main()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The above code is calling the Calendar API with the credentials that have already been setup, it will fetch all the calendars.&lt;/p&gt;

&lt;p&gt;When the above code is run for the first time, a google auth link will be opened in default browser seeking permission to access the calendar, after the consent is provided a token is saved which can be used for making requests in future to the Google Calendar API.&lt;/p&gt;

&lt;p&gt;Once the code has run, in the terminal you will see on the console the names of all the calendars that you have created.&lt;/p&gt;

&lt;p&gt;If you want to do more things, we have created an in-depth article with screenshots - &lt;a href="https://karenapp.io/articles/2019/07/how-to-automate-google-calendar-with-python-using-the-calendar-api/"&gt;how to automate google calendar with python&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The entire source code can be found &lt;a href="https://github.com/karenapp/google-calendar-python-api"&gt;here on our github&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>googlecalendar</category>
      <category>python</category>
    </item>
  </channel>
</rss>
