LoginSignup
6
4

More than 1 year has passed since last update.

Python: Google Drive APIで共有ドライブにアクセスする

Last updated at Posted at 2021-09-12

はじめに

Google Drive APIで共有ドライブにアクセスする方法について記述します。
通常のドライブとはパラメータ指定が異なるので、解決するまで時間がかかりました。

準備1(セットアップ)

Google Drive APIで通常のドライブにアクセスできるようにします。
下記の記事がわかりやすかったです。

この記事の「パッケージインストール」「OAuthクライアントID取得」「Google Drive APIを有効にする」を実施します。
※私のスキルではPyDriveで共有ドライブにアクセスできなかったので使っていません。
※「OAuthクライアントID取得」の最後でダウンロードした「client_secret_~.json」を「credentials.json」にリネームします。

準備2(テスト)

「credentials.json」と下記の記事の「quickstart.py」を同じフォルダに置きます。

「quickstart.py」を実行すると「通常」のドライブにあるファイルが10個表示されます。

> py quickstart.py
Files:
ファイル名1 (ファイルID1)
ファイル名2 (ファイルID2)
...

次に、(「通常」のドライブにある)指定したフォルダのファイルを表示するよう「quickstart.py」を改造します。
1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxにフォルダIDを記述します。)

quickstart.py(フォルダ指定)
    # Call the Drive v3 API
    query = "'1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' in parents"
    results = service.files().list(
        q=query,
        pageSize=10, fields="nextPageToken, files(id, name)").execute()
    items = results.get('files', [])

    if not items:
        print('No files found.')
    else:
        print('Files:')
        for item in items:
            print(u'{0} ({1})'.format(item['name'], item['id']))

これで、指定したフォルダのファイルを表示するようになりました。

共有ドライブのファイルを表示する

上記のプログラムで、フォルダIDの代わりに共有ドライブのID(あるいは共有ドライブの中にあるフォルダのID)を指定すると、「No files found.」になります。
共有ドライブの中にあるファイルを表示するには、以下のように記述します。
0xxxxxxxxxxxxxxxxxxに共有ドライブIDを指定します。)

quickstart.py(共有ドライブ指定)
    # Call the Drive v3 API
    results = service.files().list(
        driveId="0xxxxxxxxxxxxxxxxxx",
        corpora="drive",
        includeItemsFromAllDrives=True,
        supportsTeamDrives=True,
        pageSize=10, fields="nextPageToken, files(id, name)").execute()
    items = results.get('files', [])

    if not items:
        print('No files found.')
    else:
        print('Files:')
        for item in items:
            print(u'{0} ({1})'.format(item['name'], item['id']))

共有ドライブにファイルをアップロードする

quickstart.py(ファイルアップロード)
    # Call the Drive v3 API
    drive_id = '0xxxxxxxxxxxxxxxxxx'
    file_name = 'test.xlsx'
    file_path = './'

    # 共有ドライブにファイルが存在するかチェック
    query = "name = '{}' and trashed=false".format(file_name)
    res = service.files().list(
        q=query,
        driveId=drive_id,
        corpora="drive",
        includeItemsFromAllDrives=True,
        supportsTeamDrives=True
    ).execute()

    # アップロードするファイル
    file_metadata = {
        'name': file_name,
        'parents': [drive_id]
    }
    media = MediaFileUpload(
        file_path + file_name, 
        mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    )

    # ファイルの有無によって作成または更新
    if len(res['files']) == 0:
        res2 = service.files().create(
            body = file_metadata,
            media_body = media,
            fields = 'id',
            supportsTeamDrives = True
        ).execute()
    else:
        res2 = service.files().update(
            fileId = res['files'][0]['id'],
            media_body = media,
            fields = 'id',
            supportsTeamDrives = True
        ).execute()
6
4
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
6
4