12

Google Calendar API v3 を Python クライアントで動作させる方法について、誰かが明確に説明してくれませんか? 具体的には、OAuth の初期段階で非常に混乱します。自分のカレンダーにアクセスし、それを読み、変更するだけです。Google は、アプリを構成するために次のコードを提供しています。

import gflags
import httplib2

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

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='YOUR_CLIENT_ID',
    client_secret='YOUR_CLIENT_SECRET',
    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.
storage = Storage('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.
service = build(serviceName='calendar', version='v3', http=http,
       developerKey='YOUR_DEVELOPER_KEY')

しかし、(a)それは私にはまったく意味がありません。コメントの説明がひどい。(b) 変数に何を入れたらいいのかわからない。プログラムを Google に登録し、サービス アカウント キーにサインアップしました。しかし、私に与えられたのは、ダウンロードするための暗号化されたキー ファイルとクライアント ID だけでした。「developerKey」とは何か、または「client_secret」とは何かわかりませんか? それが鍵ですか?もしそうなら、実際には暗号化されたファイルに含まれているので、どうすれば取得できますか? 最後に、私の API 使用の比較的単純な目標 (つまり、マルチユーザー、マルチアクセス操作ではない) を考えると、これを行うためのより簡単な方法はありますか? ありがとう。

4

2 に答える 2

13

これを行う簡単な (読み: 私が行った方法) 方法は、サービス アカウントの代わりに Web アプリケーションを作成することです。どんな種類の Web アプリケーションも必要ないので、これは奇妙に聞こえるかもしれませんが、私はこれをあなたと同じ方法で使用します - 自分のカレンダーにクエリを作成する/イベントを追加するなど. - すべてコマンド ラインから、Web アプリとのやり取りは一切必要ありません。サービス アカウントを使ってそれを行う方法はいくつかありますが (実際にそのルートに行きたい場合は、いじってみます)、これは今のところうまくいきました。

Web アプリケーションを作成すると、上記のすべての情報が得られます (補足: 上記のサンプル コードは Web アプリケーションに基づいています。サービス アカウントを使用するにFLOWは、呼び出すflow_from_clientsecrets必要があり、さらに調整を行う必要があります。ここで)。したがって、このセクションに記入することができます。

FLOW = OAuth2WebServerFlow(
    client_id='YOUR_CLIENT_ID',
    client_secret='YOUR_CLIENT_SECRET',
    scope='https://www.googleapis.com/auth/calendar',
    user_agent='YOUR_APPLICATION_NAME/YOUR_APPLICATION_VERSION')

API コンソールに表示される値を入力できるようになりました ( =文字列client_id全体、 = クライアント シークレットは同じで、必要なものは何でもかまいません)。行については、API コンソールのセクションの下にある API キーです(ラベルは):Client IDclient_secretscopeuser_agentservicedeveloperKeySimple API AccessAPI key

service = build(serviceName='calendar', version='v3', http=http, 
    developerKey='<your_API_key>')

次に、次のような簡単なチェックを追加して、機能したかどうかを確認できます。

events = service.events().list(calendarId='<your_email_here>').execute()
print events

これを実行すると、ブラウザ ウィンドウがポップアップ表示され、認証フローを完了することができます。これは、すべての認証が Google によって処理され、認証応答情報が に保存されることを意味しcalendar.datます。そのファイル (スクリプトと同じディレクトリに保存されます) には、サービスが現在使用する認証情報が含まれています。それがここで起こっていることです:

storage = Storage('calendar.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
  credentials = run(FLOW, storage)

そのファイルを探して内容を検証することにより、有効な資格情報の存在をチェックします (実装を容易にするために、これはすべて抽象化されています)。認証後、ifステートメントが評価されFalse、再度認証する必要なくデータにアクセスできるようになります。

うまくいけば、それがプロセスにもう少し光を当てます-簡単に言えば、Webアプリケーションを作成し、そこからのパラメーターを使用し、一度認証したら、それを忘れてください。見落としがちな点も多々あるかと思いますが、ご参考になれば幸いです。

于 2012-12-28T06:23:32.100 に答える