Drkcore

02 02 2012 Python Tweet

Pythonで Google Tasks API を使う

Todo管理にはGoogle Tasksを使っている。家でも職場でもタスクを追加できて一元管理できるので便利なんだけど CUIでもできたらもっと便利だろうとAPIを触ってみている。

家では普通にDeveloper's Guideの通りに動くんだが、職場のプロキシが超えられないので調べてみたらoauth2client.toolsのrunがダメらしい。

さらに追っかけてみるとflow.step2_exchange(code)のところの第二引数にproxyを設定したhttpを渡さないからこけていた。

run関数使わなければいいじゃんってことで書きなおした。ついでにFLAGも必要なくなった。

import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow, FlowExchangeError

FLOW = OAuth2WebServerFlow(
  client_id='####',
  client_secret='####',
  scope='https://www.googleapis.com/auth/tasks',
  user_agent='gtask-client/1.0')

storage = Storage('tasks.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
  authorize_url = FLOW.step1_get_authorize_url('oob')
  print 'Go to the following link in your browser:'
  print
  print '    ' + authorize_url
  print
  code = None
  code = raw_input('Enter verification code: ').strip()
  try:
    http = httplib2.Http(proxy_info = httplib2.ProxyInfo(httplib2.socks.PROXY_TYPE_HTTP, '####', 8080))
    credential = FLOW.step2_exchange(code, http)
  except FlowExchangeError, e:
    sys.exit('Authentication has failed: %s' % e)
  storage.put(credential)
  credential.set_store(storage)  
  credentials = credential

http = httplib2.Http(proxy_info = httplib2.ProxyInfo(httplib2.socks.PROXY_TYPE_HTTP, '####', 8080))
http = credentials.authorize(http)

service = build(serviceName='tasks', version='v1', http=http,
                developerKey='####')

tasklists = service.tasklists().list().execute()

for tasklist in tasklists['items']:
  print tasklist['title']

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021