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

Https://code.google.com/apis/console

Active Services

Allow API Access

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

#!/usr/local/bin/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]
#時間偏移量30秒
time_shift=timedelta(0,30)
#日期時間轉換為 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(
    client_id='xxxxxxxxxxxx.apps.googleusercontent.com',
    client_secret='_KCEjK9o_BdFg*********_L',
    scope='https://www.googleapis.com/auth/calendar',
    user_agent='YOUR_APPLICATION_NAME/YOUR_APPLICATION_VERSION')

# 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,
       developerKey='AIzaSyCyY2E21FT4Q6q_8wKqF6sC*********-E')

# 新增-新的事件
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,
sendNotifications=True
).execute()

print created_event['id']

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

參考來源:http://code.google.com/intl/zh-TW/apis/calendar/v3/getting_started.html

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

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s