Operate limited shared Google Calendar with Lambda (Python) [cloudpack Osaka]

It's a memorandum because I was sick. Operate the Google calendar that is shared exclusively with Lambda (Python). _ I did it at work, but I will deliver the tragedy that Google's library was updated one week before the start, the fastest in Japan. _

Required library

Below, please put in with pip

Creating a service account

In order to access Google Calendar, you need to create an authentication user called a service account.

Access API Manager from the Google Developer Console (https://console.developers.google.com/).

API_ライブラリ_-_msp-calendar.png

Enable Calendar API. Calendar_API_-_msp-calendar.png

Go to "Credentials" from "API Manager" and create a service account key. 認証情報_-_msp-calendar.png

Please enter your name and service account ID. You will use the service account ID later. Also, this time specify the key type p12. サービス_アカウントキーの作成_-_msp-calendar 2.png

Since the p12 file is DL, convert it to pem openssl pkcs12 -in key.p12 -nodes -nocerts > key.pem

Calendar sharing settings

Add the service account you created to the Google Calendar shared users. I was addicted to the official documentation because I couldn't find the description here. There was a description like how to spoof the user, but it seems that it can not be operated unless it is added.

  1. "Settings" from the gear on the upper right of Google Calendar
  2. Select the "Calendar" tab and go to "Share: Edit Settings" アイレット株式会社_-カレンダー-_設定.png
  3. Add the service account you just created アイレット株式会社_-カレンダー-_共有.png

Source code

import httplib2
from apiclient import discovery
import oauth2client
from oauth2client.service_account import ServiceAccountCredentials
from datetime import datetime as dt

SERVICE_ACCOUNT_ID = "[email protected]" #Acquired service account

def lambda_handler(event, context):
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build(
        'calendar',
        'v3',
        http=http
    )

    events = service.events().list(calendarId=CALENDAR_ID).execute()

    return events.get('items', [])


def get_credentials():
    scopes = 'https://www.googleapis.com/auth/calendar'
    credentials = ServiceAccountCredentials.from_p12_keyfile(
        SERVICE_ACCOUNT_ID,
        'key.pem',
        scopes=scopes
    )

    return credentials

After that, it's okay if you upload it to Lambda as lambda-uploader or compress it yourself!

Recommended Posts

Operate limited shared Google Calendar with Lambda (Python) [cloudpack Osaka]
Operate TwitterBot with Lambda, Python
[GCP] Operate Google Cloud Storage with Python
Upload files to Google Drive with Lambda (Python)
Operate Kinesis with Python
Operate Blender with Python
Operate Excel with Python (1)
Operate Excel with Python (2)
Operate Excel with Python openpyxl
How to extract any appointment in Google Calendar with Python
Study Python with Google Colaboratory
[Note] Operate MongoDB with Python
Access Google Drive with Python
Python: Extract file information from shared drive with Google Drive API
AWS Lambda + Twilio Make Google Calendar reminders call with voice notifications
Create AtCoder Contest appointments on Google Calendar with Python and GAS
Operate a receipt printer with python
Try to operate Facebook with Python
Face detection with Lambda (Python) + Rekognition
Operate ECHONET Lite appliances with Python
Notify HipChat with AWS Lambda (Python)
Use PostgreSQL with Lambda (Python + psycopg2)
I tried updating Google Calendar with CSV appointments using Python and Google APIs
[AWS] Using ini files with Lambda [Python]
Operate smartlife power supply with python (de-IFTTT)
Shared screen screenshot exe app with python
Manipulate DynamoDB data with Lambda (Node & Python)
Register users with Google Admin SDK (python)
Connect to s3 with AWS Lambda Python
Get holidays with the Google Calendar API
[Python] Automatically operate the browser with Selenium
Operate home appliances with Python and IRKit
Try assigning or switching with Python: lambda
Upload images to Google Drive with Python
Python + Selenium + Headless Chromium with aws lambda