howdylikes

Google Developersってわかりづらいよね

Google Apps Activity APIの使い方

f:id:howdy39:20150920232232p:plain

developers.google.com

本APIはユーザーのAppsの活動状態を取得するためのAPIです。
2015/9/21時点で利用できるのはDriveだけのようです。
Appsとついていますが普通のGmailアカウントでも使えます。

※似たようなAPIとしてfor work向けにReports APIが提供されています。
こちらは管理者権限で全ユーザーの活動が抜けます。
https://developers.google.com/admin-sdk/reports/v1/guides/manage-audit-drive

Activitiesの取得

  1. 以下のURLにいきます。
    https://developers.google.com/google-apps/activity/v1/reference/activities/list
  2. Authorize requests using OAuth 2.0をクリックして認可します。
  3. sourcedrive.google.com
    drive.ancestorIdにフォルダID(URLに出てくる乱数です)を入力しEXECUTEを押下します。
{
 "activities": [
  {
   "combinedEvent": {
    "user": {
     "name": "Tatsuya Nakano"
    },
    "primaryEventType": "create",
    "additionalEventTypes": [
     "create"
    ],
    "eventTimeMillis": "1442839844313",
    "target": {
     "id": "1A8yUWLtHkIZpS9wTgJqNv96ZL3dGTvljlKEw_KqkgbY",
     "name": "testdoc",
     "mimeType": "application/vnd.google-apps.document"
    }
   },
   "singleEvents": [
    {
     "user": {
      "name": "Tatsuya Nakano"
     },
     "primaryEventType": "create",
     "additionalEventTypes": [
      "create"
     ],
     "eventTimeMillis": "1442839844313",
     "target": {
      "id": "1A8yUWLtHkIZpS9wTgJqNv96ZL3dGTvljlKEw_KqkgbY",
      "name": "testdoc",
      "mimeType": "application/vnd.google-apps.document"
     }
    }
   ]
  },
  {
   "combinedEvent": {
    "user": {
     "name": "Tatsuya Nakano"
    },
    "primaryEventType": "create",
    "additionalEventTypes": [
     "create",
     "move"
    ],
    "eventTimeMillis": "1442838216498",
    "target": {
     "id": "0B-GnwaTpiZSxWFN1c29fbXUtTUE",
     "name": "test",
     "mimeType": "application/vnd.google-apps.folder"
    },
    "move": {
     "addedParents": [
      {
       "id": "0AOGnwaTpiZSxUk9PVA",
       "isRoot": true
      }
     ]
    }
   },
   "singleEvents": [
    {
     "user": {
      "name": "Tatsuya Nakano"
     },
     "primaryEventType": "create",
     "additionalEventTypes": [
      "create",
      "move"
     ],
     "eventTimeMillis": "1442838216498",
     "target": {
      "id": "0B-GnwaTpiZSxWFN1c29fbXUtTUE",
      "name": "test",
      "mimeType": "application/vnd.google-apps.folder"
     },
     "move": {
      "addedParents": [
       {
        "id": "0AOGnwaTpiZSxUk9PVA",
        "isRoot": true
       }
      ]
     }
    }
   ]
  }
 ],
 "nextPageToken": ""
}

フォルダやファイルを作ったという活動が取得できました。
drive.ancestorIdにはフォルダIDを入れてフォルダ配下の活動の取得
drive.fileIdにはファイルIDを入れてファイルの活動の取得
がそれぞれ可能なようです。

groupingStrategyの設定次第でcombinedEventだけ取得可能ですがちょっとややこしいです。
e.g. 2ファイルを対象にAフォルダからBフォルダに移動した際
driveUi(default)の場合、combinedEventにはAからBに移動したという1件が返却され、simpleEventsには2ファイルがAからBに移動したという2件が返却されます。
noneの場合、combinedEventにAからBに移動したという2件が別で返却されます。

また不可解な点としてファイルを新規作成→すぐリネームした際にnoneだとリネームも取れましたがdriveUiだとリネーム後のファイル名で新規作成した扱いになっていました。。。
ちょっと動きが怪しいので事前にしっかり検証することをお勧めします。

総括

一般のGmailアカウントでも使えるというのはReports APIにない利点ですが、Driveでしか現状使えないことで使い道がかなり限定されると思います。