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. _
Below, please put in with pip
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/).
Enable Calendar API.
Go to "Credentials" from "API Manager" and create a service account key.
Please enter your name and service account ID. You will use the service account ID later. Also, this time specify the key type p12.
Since the p12 file is DL, convert it to pem
openssl pkcs12 -in key.p12 -nodes -nocerts > key.pem
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.
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