#動作環境
- Google API/Drive API
- google api client for python
- python3
- atom
#背景
Google API で DriveAPI を使用して自分のGoogle Driveからファイルを取得/アップロードしようとしています。その際にServer to ServerにおけるOAuth2を利用したいと考えています。
こちらのサンプルコードを利用しているのですが、access token が払い出されません。
・Server to Server
https://developers.google.com/api-client-library/python/auth/service-accounts
・実行結果:
{'token': None, 'expiry': None, '_scopes': ['https://www.googleapis.com/auth/drive'],(以下省略)
実際にDrive API でファイル名を取得しようとしますが、やはり取得できませんでした。
・以下のサンプルコードを利用しています。
https://developers.google.com/drive/api/v3/search-parameters
※取得結果が「None」になります。
切り分けとして、認証方式をInstalled Application のものに変えたところ、うまく動いたので、認証ができていないのだと、想定しています。
・Installed Application のサンプルコード
https://developers.google.com/api-client-library/python/auth/installed-app
より詳しく確認したところ、Server to Server の方式では、G Suite(GoogleApps)の管理者コンソール上で、サービスアカウントにGoogle Driveの権限(スコープ?)を割り当てる必要があるように見えます。
https://developers.google.com/api-client-library/python/auth/service-accounts
Go to your Google Apps domain’s Admin console.
Select Security from the list of controls. If you don't see Security listed, select More controls from the gray bar at the bottom of the page, then select Security from the list of controls. If you can't see the controls, make sure you're signed in as an administrator for the domain.
Select Advanced settings from the list of options.
Select Manage third party OAuth Client access in the Authentication section.
In the Client name field enter the service account's Client ID.
In the One or More API Scopes field enter the list of scopes that your application should be granted access to. For example, if your application needs domain-wide access to the Google Drive API and the Google Calendar API, enter: https://www.googleapis.com/auth/drive, https://www.googleapis.com/auth/calendar.
Click Authorize.
ただ、ここで問題が生じました。
GSuiteの管理者アカウントはフリーのgmailアカウントでは使用できないようで、おそらく、ここが問題だと考えています。
#質問:
フリーのgmailアドレスでServer to Serverの方式は使用できないのでしょうか?
もし、使用している方がいれば、教えていただきたく。
※或いはもし他に原因があれば、指摘いただきたいです。
よろしくお願いします。
#追加切り分け ※11/8追記
頂いた回答を元にして、もう少し切り分けしてみました。
結果、OAuth2の認証は動いているようですが、出力結果としては初期のGettingStartのファイルしか出力されていないことが分かりました。なので、やはり、認証関連で何かがおかしいように見えます。。。
####実行内容
#encoding : utf-8 # Authorize server-to-server interactions from Google Compute Engine. import json #1 認証系 from google.oauth2 import service_account SCOPES = ['https://www.googleapis.com/auth/drive'] SERVICE_ACCOUNT_FILE = (サービスアカウントのjsonファイルへのパス) credentials = service_account.Credentials.from_service_account_file( SERVICE_ACCOUNT_FILE, scopes=SCOPES) print('CREDENTIALs:',credentials.__dict__) #2 サービスの構築:実行したいAPI種類を選択し、認証はこのタイミングで実施される from apiclient.discovery import build service = build('drive', 'v3', credentials = credentials) print('SERVICE:',service.__dict__)\ page_token = None while True: response = service.files().list(spaces='drive', fields='nextPageToken, files(id, name)', pageToken=page_token).execute() print(response.get('nextPageToken')) for file in response.get('files', []): # Process change ; 最初の流儀 #print('Found file: %s (%s)' % (file.get('name'), file.get('id'))) print(file) page_token = response.get('nextPageToken', None) if page_token is None: break
####実行結果
(CREDENTIALS、SERVICE は省略) None ←nextPageToken。 {'id': (※GoogleDriveのファイルID), 'name': 'Getting started'}
####原因追及
こちらが参考になりそうなので、これを元に、もう少し、調査してみようと思います。
https://www.daimto.com/google-drive-api-with-a-service-account/
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/08 10:03