LoginSignup
122
118

More than 5 years have passed since last update.

PythonからOAuth2.0を利用してスプレッドシートにアクセスする

Last updated at Posted at 2015-06-06

背景

Google APIの利用について、GoogleのユーザーIDとパスワードでのアクセスが2015年5月に禁止された。現在はOAuth2.0を利用してアクセスしなければならない。PythonからOAuth2.0での認証に関して、意外と情報が少なく手間取ったのでやり方を残しておく。
本記事はスプレッドシートに関してのものだが、gdataライブラリを利用する他のAPIにも同じように認証が可能なのではないかと思う(未確認)。

環境

python 2.7.5
MacOS X 10.9.5

OAuth2.0での認証方法

OAuth2.0での認証のためには、いくつか準備しなければいけないことがある。
順番に手順を残しておく。

1. Drive APIの有効化

スプレッドシートは、Google Apps APIのDrive APIを利用して操作することになるので、以下の操作で必要なAPIを有効にする。

Developers Consoleにアクセスして、プロジェクトを選択。
プロジェクトが存在しない場合は、適当な名前で一つ作成する。

プロジェクト.png

左側のメニューから「API」を選ぶ。

概要_-_neoimagewall.png

「Drive API」は右の方。

API_ライブラリ_-_neoimagewall.png

「APIを有効にする」

Drive_API_-_API_Project.png

有効になった。

Drive_API_-_API_Project.png

2. OAuth用クライアントIDの作成

次は、OAuthで利用するためのクライアントIDと鍵を作成しなければいけない。やり方を載せる。

同じくDevelopers Consoleでプロジェクトを開いた画面で、左側のメニューから「認証情報」を選択。

Drive_API_-_API_Project.png

「新しいクライアントIDを作成」

認証情報_-_kumaproduct.png

「サービス アカウント」を選んで「クライアントIDを作成」

認証情報_-_kumaproduct.png

ローカルにjsonファイルがダウンロードされ、完了のダイアログが表示される。
(今回の手順ではこのjsonファイルは利用しない)

認証情報_-_kumaproduct.png

クライアントIDが発行されたことを確認。
「新しいP12キーを生成」を選び、秘密鍵(.p12)をダウンロードする。

認証情報_-_kumaproduct.png

秘密鍵がローカルに保存された。(保存された秘密鍵を「MyProject.p12」とする)

認証情報_-_kumaproduct.png

3. スプレッドシートの共有設定

上記の手順で作成したクライアントIDに対して、SpreadSheet側で共有設定を行う必要がある。

まずは、発行したクライアントIDのメールアドレスを控えておく。

9184c188-2ade-9e97-7d1e-fb1ed6a2eaf0.png

APIでアクセスしたいスプレッドシートを開いて、「共有」を選択。

無題スプレッドシート_-_Google_スプレッドシート.png

発行されたメールアドレスを入力して「送信」。

無題スプレッドシート_-_Google_スプレッドシート.png

これで共有設定が完了した。

4. ソースコード

さてようやく準備が整った。pythonのコードでアクセスする。

まず、必要なライブラリをインストール。

$ pip install gdata oauth2client

認証部分は、次のようなソースになる。

oauth2.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from oauth2client.client import SignedJwtAssertionCredentials
import gdata.spreadsheets.client

# 認証に必要な情報
client_email = "123456789000-abc123def456@developer.gserviceaccount.com" # 手順2で発行されたメールアドレス
with open("MyProject.p12") as f: private_key = f.read() # 手順2で発行された秘密鍵

# 認証情報の作成
scope = ["https://spreadsheets.google.com/feeds"]
credentials = SignedJwtAssertionCredentials(client_email, private_key,
    scope=scope)

# スプレッドシート用クライアントの準備
client = gdata.spreadsheets.client.SpreadsheetsClient()

# OAuth2.0での認証設定
auth_token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
auth_token.authorize(client)

# ---- これでライブラリを利用してスプレッドシートにアクセスできる ---- #

# ワークシートの取得
sheets = client.get_worksheets("1TAVVsyhCM_nprkpa0-LGWBheaXt_ipX84fIIhJw2fa0") # スプレッドシートIDを指定
for sheet in sheets.entry:
    print sheet.get_worksheet_id(), sheet.title

最後のprint文がこんな感じで出力されたら正常にアクセスできた証拠。

od6 <ns0:title xmlns:ns0="http://www.w3.org/2005/Atom">&#12471;&#12540;&#12488;1</ns0:title>

遭遇したエラー

PKCS12 format is not supported by the PyCrypto library.

エラー内容

PKCS12 format is not supported by the PyCrypto library. Try converting to a "PEM" (openssl pkcs12 -in xxxxx.p12 -nodes -nocerts > privatekey.pem) or using PyOpenSSL if native code is an option.

以下の部分で起きたエラー。

with open("MyProject.p12") as f

対応

エラー文に載っているようにして秘密鍵の形式を変更すればよい。

$ openssl pkcs12 -in MyProject.p12 -nodes -nocerts > MyProject.pem
Enter Import Password: #「notasecret」と入力

それからコード中で読み込むファイルを変える。

with open("MyProject.pem") as f

参考

GoogleAppEngine - Google App Engine for PHPでGoogle Drive APIを使う - Qiita
http://qiita.com/hikoalpha/items/04ef84cd5f035ff64f23

python - Using Spreadsheet API OAuth2 with Certificate Authentication - Stack Overflow
http://stackoverflow.com/questions/20209178/using-spreadsheet-api-oauth2-with-certificate-authentication/20211057#20211057

Using OAuth 2.0 for Server to Server Applications | Google Identity Platform | Google Developers
https://developers.google.com/identity/protocols/OAuth2ServiceAccount?hl=ja

OAuth 2.0   |   API Client Library for Python   |   Google Developers
https://developers.google.com/api-client-library/python/guide/aaa_oauth?hl=ja

google app engine - SignedJwtAssertionCredentials on AppEngine doesn't recognize PEM key - Stack Overflow
http://stackoverflow.com/questions/17993604/signedjwtassertioncredentials-on-appengine-doesnt-recognize-pem-key

gdata: gdata.spreadsheets.client Namespace Reference - doxygen documentation | Fossies Dox
http://fossies.org/dox/gdata-2.0.18/namespacegdata_1_1spreadsheets_1_1client.html

122
118
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
122
118