Skip to content

Instantly share code, notes, and snippets.

@chwnam
Created February 28, 2015 20:54
Show Gist options
  • Save chwnam/e91949eb3bdabbb74d05 to your computer and use it in GitHub Desktop.
Save chwnam/e91949eb3bdabbb74d05 to your computer and use it in GitHub Desktop.
Google Calendar API Example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import httplib2
import json
from apiclient.discovery import build
from oauth2client import tools
from oauth2client.file import Storage
from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import run_flow
parser = argparse.ArgumentParser(parents=[tools.argparser])
flags = parser.parse_args()
# https://console.developers.google.com/project
# 먼저 프로젝트 등록하고 OAuth2.0 인증 키를 발급받아야 한다.
flow = flow_from_clientsecrets('msps_auth.json', scope='https://www.googleapis.com/auth/calendar')
storage = Storage('calendar.dat')
credentials = storage.get()
if credentials is None or credentials.invalid is True:
credentials = 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)
# Build a service object for interacting with the API.
service = build(serviceName='calendar', version='v3', http=http, credentials=credentials)
# 내 캘린더 아이디.
calendar_id = "3o40@group.calendar.google.com"
# 캘린더 전체 목록
#request = service.calendarList().list()
#response = request.execute()
# 캘린더 정보
#request = service.calendars().get(calendarId=calendar_id )
#response = request.execute()
# 캘린더 일정
request = service.events().list(calendarId=calendar_id)
response = request.execute()
# 파일 덤프
with open('calendar_list.json', 'w') as f:
json.dump(response, f, indent=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment