LoginSignup
5
5

More than 5 years have passed since last update.

Google Drive on Google Apps for Work でドメイン内の全ユーザーのファイルの共有状態を取得する

Last updated at Posted at 2015-10-22

Google Apps for WorkのUnlimitedじゃないプランなので監査とかできないんだけど、ドメイン内の誰かがうっかり間違って公開したファイルが公開したままになってたらこわいよね。というわけで作りました。

ちなみに、Unlimitedの監査機能でもファイルの公開状態の取得はできないっぽい(?)んですけど、ご存知でしたら教えてください。

ハマったポイント

Googleの公式ドキュメントにあるサンプルが動かない

クラス名やメソッド名が変わってて全然動かん。
gem google-api-rubyはalphaバージョンみたいだし、まあしゃーない…か?

permissionsをキーに検索できない

まあそういうもんらしい。なので、全ファイルのpermissionをいちいち取得してます。
「え、できるだろ?」という方がいらっしゃいましたら教えていただけるとありがたいです…

自分以外の他のユーザーのファイルが探せない

管理者ユーザーであっても「誰でアクセスするか」を指定しないといけない。これがわからず、だいぶ時間を無駄にした。

  • Service Account を使う。
  • Google Apps for Work の 管理コンソール > セキュリティ > 詳細設定 > API クライアント アクセスを管理する からAPIクライアントを許可(登録)する。
  • 「誰でアクセスするか」を指定する。

要するに下記の :sub のところ。

  client.authorization = Signet::OAuth2::Client.new(
    :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
    :audience             => 'https://accounts.google.com/o/oauth2/token',
    :scope                => ['https://www.googleapis.com/auth/drive.readonly', 'https://www.googleapis.com/auth/admin.directory.user.readonly'],
    :issuer               => issuer,
    :signing_key          => key,
    :sub                  => 'user@example.com'
  )

あとがき

fork元の https://github.com/morimorihoge/google-drive-permission-search を公開してくださった morimorihoge さん、ありがとうございます。
割と原型を留めていませんが…

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