Ubuntu install google calendar api with python

安裝設定 google calendar api

sudo apt-get install python-setuptools
hg clone https://google-api-python-client.googlecode.com/hg/ google-api-python-client
cd google-api-python-client
sudo python setup.py install

p.s: 目前 python easy_install 也就是 python-setuptools 套件僅支援到 2.7 的版本。

登入 google apis console 並啟用 google-calendar-api


Active Services

Allow API Access

Calendar API 新增日曆事件 Python 範例程式

<span style="color: #222222; font-family: 'Courier 10 Pitch', Courier, monospace;"><span style="line-height: 21px;"># -*- encoding=utf-8 -*-
</span></span>import sys
from datetime import datetime,timedelta
import gflags
import httplib2

from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run

Summary = sys.argv[1]
#日期時間轉換為 google-calendar 樣式
now_str = str(datetime.now()+time_shift).replace(" ","T")

FLAGS = gflags.FLAGS

# Set up a Flow object to be used if we need to authenticate. This
# sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with
# the information it needs to authenticate. Note that it is called
# the Web Server Flow, but it can also handle the flow for native
# applications
# The client_id and client_secret are copied from the API Access tab on
# the Google APIs Console
FLOW = OAuth2WebServerFlow(

# To disable the local server feature, uncomment the following line:
# FLAGS.auth_local_webserver = False

# If the Credentials don't exist or are invalid, run through the native client
# flow. The Storage object will ensure that if successful the good
# Credentials will get written back to a file.
# 認證資訊 token 檔案的存放路徑
storage = Storage('/root/calendar.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
  credentials = run(FLOW, storage)

# 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. Visit
# the Google APIs Console
# to get a developerKey for your own application.
# developerKey (API Key)
service = build(serviceName='calendar', version='v3', http=http,

# 新增-新的事件
event = {
  'summary': Summary,
  'location': '台灣東洋彩光',
  'start': {
    'dateTime': now_str,
    'timeZone': 'Asia/Taipei'
  'end': {
    'dateTime': now_str,
    'timeZone': 'Asia/Taipei'

# calendarID 要登錄事件日曆的 ID
created_event = service.events().insert(calendarId='mis@toyoink-ct.com.tw', body=event,

print created_event['id']

p.s:上述的認證動作會開啟網頁讓使用者,登打 google 的帳號密碼,驗證成功後會建立一個連線的 token 資訊檔案。在第一次驗證成功後,就不需要在次登打帳號和密碼了。



One thought on “Ubuntu install google calendar api with python

  1. 引用通告: Use google calendar api to Nagios send notification by SMS | jerry-yang work's logs



WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )


您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s