0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GoogleDriveにbotが人のふりをしてファイルをアップロードする(Google Workspace限定)

Posted at

Googleのサービスアカウントでドメイン全体の権限委譲をする

毎日Google DriveにファイルをアップロードするBotを書いていて、
ある、ファイル容量でもうあげれないよ?って警告がでて動かなくなってきた。
そこで、ドメイン全体の権限委譲が必要となって解決した話。

権限委譲をすることによって、委譲した人となってGoogle Driveにアクセスできるようになる。
つまり、ファイル容量の制限がサービスアカウントではなく、workspaceのユーザーのものになる。

ドメイン全体の権限委譲が必要

もろもろ調べていると、サービスアカウントはgoogleから見るとDummyアカウントとしてみなされていて

  • 特定のドメインに追加することもダメ
  • owner権限をbotからユーザーにすることもダメ

な模様。Google Workspaceから設定が必要なので、まずはWorkspaceの特権権限をもらい、
ドメイン全体の権限委譲 の手順に従って設定をします。

ドメイン移譲をコードに反映する

通常のサービスアカウントの認証部分にdelegateを。追加します。
create_delegateをしておくことによって、このbotにメールアドレスの人の権限が委譲され、Driveの履歴もメールアドレスの人のものになる。

from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

scope =['https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('creds.json', scope)

#この行を追加
delegated_credentials = creds.create_delegated('your-email@gmail.com')

service = build('drive', 'v3', credentials=delegated_credentials)

まとめ

多分同様の問題に当たる人がそんなに多くないのか、結構調べるのに時間がかかってしまった。
これでファイル容量を気にせずdriveにアップロードできるようになった。よかったよかった

参考リンク:stackoverflow

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?