質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

1回答

4759閲覧

PythonでGoogle Drive APIを使ってファイルの変更履歴にアクセスできない

asiangeorge

総合スコア8

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/10/21 07:59

編集2020/10/21 08:03

前提・実現したいこと

Google Drive API v3を使ってPythonプログラムからGoogle Drive上のファイルの変更履歴を取得したいです。
変更履歴取得のところでエラーがでて、ログインが必要なようですが方法が分かりません。

発生している問題・エラーメッセージ

下に示すコードのservice.changes().list(pageToken=page_token, spaces='drive').execute()部分で以下のエラーが出ました。

File "/Users/***/.pyenv/versions/3.8.2/lib/python3.8/site-packages/googleapiclient/http.py", line 907, in execute raise HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/drive/v3/changes?pageToken=XXXXXX&spaces=drive&alt=json returned "Invalid Value">

エラー文中のhttps://www.googleapis.com/drive/v3/changes?pageToken=...を開くと、

{ "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Login Required" } }

が表示されました。

該当のソースコード

Python

1import pickle, os.path 2 3from googleapiclient.discovery import build 4from google_auth_oauthlib.flow import InstalledAppFlow 5from google.auth.transport.requests import Request 6from google.oauth2 import service_account 7 8 9# ========================== Constants ========================== 10SCOPES = [ 11 'https://www.googleapis.com/auth/drive', 12 'https://www.googleapis.com/auth/documents' 13] 14# =============================================================== 15 16 17def _get_credidential_oauth(): 18 """ 19 Return type: <class \'google.oauth2.credentials.Credentials\'> 20 21 The file token.pickle stores the user's access and refresh tokens, and is 22 created automatically when the authorization flow completes for the first 23 time. 24 """ 25 26 creds = None 27 28 if os.path.exists('token.pickle'): 29 with open('token.pickle', 'rb') as token: 30 creds = pickle.load(token) 31 # If there are no (valid) credentials available, let the user log in. 32 if not creds or not creds.valid: 33 if creds and creds.expired and creds.refresh_token: 34 creds.refresh(Request()) 35 else: 36 flow = InstalledAppFlow.from_client_secrets_file( 37 'credentials.json', SCOPES) 38 creds = flow.run_local_server(port=0) 39 # Save the credentials for the next run 40 with open('token.pickle', 'wb') as token: 41 pickle.dump(creds, token) 42 43 return creds 44 45 46def main(): 47 creds = _get_credidential_oauth() 48 service = build('drive', 'v3', credentials=creds) 49 service_doc = build('docs', 'v1', credentials=creds) # 使ってないです 50 51 edit_hists = [] 52 page_token = service.changes().getStartPageToken().execute() 53 while page_token is not None: 54 response = service.changes().list(pageToken=page_token, spaces='drive').execute() # エラー箇所 55 for change in response.get('changes'): 56 edit_hists.append(change) 57 58 if 'newStartPageToken' in response: 59 # Last page, save this token for the next polling interval 60 saved_start_page_token = response.get('newStartPageToken') 61 page_token = response.get('nextPageToken') 62 63 page_token = response.get('nextPageToken') 64 65 66if __name__ == '__main__': 67 main() 68

credentials.jsonがログイン情報のファイルです。

参考にしたコード

Googleのサイト(https://developers.google.com/drive/api/v3/manage-changes)を参照しました。

補足情報(FW/ツールのバージョンなど)

macOS 10.15.7
Python 3.8.2
Visual Studio Code 1.50.1 + MicrosoftのPythonプラグイン v2020.9.114305

以上です。手助けいただければ幸いです。不備があったら修正しますのでお手柔らかにお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

個人情報にアクセスするときは、必ず認証が必要です。
Google の Console からアプリの登録をして Token を取得して、それを使って認証をすませてください。

oauth で検索すれば、やり方は山のように出てきます。

投稿2020/11/05 07:41

Bindi

総合スコア129

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問