掘り下げた後、OAuth2 認証に基づくサンプルをいくつか見つけました。このことから、カレンダー API にアクセスするための JWT を作成する次の簡単なサンプルを作成しました。
import httplib2
import pprint
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
# Get the private key from the Google supplied private key file.
f = file("your_private_key_file.p12", "rb")
key = f.read()
f.close()
# Create the JWT
credentials = SignedJwtAssertionCredentials(
"xxxxxxxxxx@developer.gserviceaccount.com", key,
scope="https://www.googleapis.com/auth/calendar"
)
# Create an authorized http instance
http = httplib2.Http()
http = credentials.authorize(http)
# Create a service call to the calendar API
service = build("calendar", "v3", http=http)
# List all calendars.
lists = service.calendarList().list(pageToken=None).execute(http=http)
pprint.pprint(lists)
これを Google App Engine で機能させるには、アプリで PyCrypto を有効にする必要があります。これは、app.yaml
ファイルに次を追加することを意味します。
libraries:
- name: pycrypto
version: "latest"