特定の Google Apps ユーザーのすべてのファイルをダウンロードするスクリプトを作成しています。そのユーザーを偽装するサービス アカウントを使用します (ここで提供される Python テンプレート コードを使用します(以下のコード ブロックを参照))。
def createDriveService(user_email):
"""Build and returns a Drive service object authorized with the service accounts
that act on behalf of the given user.
Args:
user_email: The email of the user.
Returns:
Drive service object.
"""
f = file(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'rb')
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key,
scope='https://www.googleapis.com/auth/drive', prn=user_email)
http = httplib2.Http()
http = credentials.authorize(http)
return build('drive', 'v2', http=http)
この機能を使用して認証に成功し、ダウンロード、ファイル リスト リクエストなどを実行できます。ただし、短時間に 4 回以上のダウンロードを実行しようとすると、次のエラーが発生します。
Traceback (most recent call last):
// snip //
File "C:\***\changeScan2.py", line 48, in createDriveService
return build('drive', 'v2', http=http)
File "build\bdist.win32\egg\oauth2client\util.py", line 120, in positional_wrapper
File "build\bdist.win32\egg\apiclient\discovery.py", line 193, in build
File "build\bdist.win32\egg\oauth2client\util.py", line 120, in positional_wrapper
File "build\bdist.win32\egg\oauth2client\client.py", line 405, in new_request
File "build\bdist.win32\egg\oauth2client\client.py", line 573, in _refresh
File "build\bdist.win32\egg\oauth2client\client.py", line 629, in _do_refresh_request
oauth2client.client.AccessTokenRefreshError: Invalid response 403
プログラムは、ダウンロードするファイルのリスト (files.list) を正常に取得し、4 つのファイルをダウンロードし (毎回認証を行います)、5 回目のダウンロードで上記のエラーを表示します。プロセス全体に 5 ~ 10 秒かかります。異なるファイルで複数回実行すると、プログラムは 5 番目のファイルをダウンロードするプロセスでエラーを返します。
アプリを必要最小限にまで切り詰め、別のファイルをダウンロードしてみましたが、同じエラーが発生しました。例外をキャッチして createDriveService 関数の指数バックオフを実装しようとしましたが、エラーは Google API クライアント ファイルにあるようで、軽減できませんでした。これは、問題を引き起こしていると思われる oauth2client\client.py のコードを表示するためのリンクです。
何か案は?