DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  • 3 Reasons Why VPs of Engineering Are Choosing Low-Code
  • Be a Better Team Player
  • The Rising Risks and Opportunities in API Security

Trending

  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  • MySQL to PostgreSQL Database Migration: A Practical Case Study
  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs
  • How to Perform Custom Error Handling With ANTLR
  1. DZone
  2. Data Engineering
  3. Databases
  4. Working With the Google Drive API: Track Changes in Individual and Team Drives

Working With the Google Drive API: Track Changes in Individual and Team Drives

Work with the Google Drive API and track changes in individual and team drives.

By 
Jord Lin user avatar
Jord Lin
·
Apr. 05, 19 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
25.9K Views

Join the DZone community and get the full member experience.

Join For Free

Developers commonly integrate with cloud storage services such as Google Drive to sync files that change between their app and users’ cloud storage. At Kloudless, we’ve seen activity monitoring used for everything from workflow automation to data loss protection.

The Kloudless unified Events API enables developers to track changes regardless of which cloud storage account a user connects. Google Drive remains one of the most popular services users connect accounts for. In this blog post, we’ll discuss the various ways to track changes in Google Drive and when to use each.

Google Drive provides three different API endpoints each capable of tracking changes in a user’s account:

  • The Drive Changes API
  • The Drive Activity API
  • The Admin SDK’s Reports API

The Changes API

The Changes API provides an efficient way to detect changes to all files, including those that have been shared with a user. Here is an example of a change resource returned when listing changes in an account:

{
  "kind": "drive#change",
  "type": "file",
  "time": "2017-09-07T09:02:26.581Z",
  "removed": false,
  "fileId": "0B648bYBbhWYoc3RhcnRlcl9maWxlX2Rhc2hlclYw",
  "file": {
   "kind": "drive#file",
   "id": "0B648bYBbhWYoc3RhcnRlcl9maWxlX2Rhc2hlclYw",
   "name": "Getting started",
   "mimeType": "application/pdf"
  }
}

The Changes API helps detect which data has changed or been removed. It doesn’t provide much information on how the change occurred, or activity other than additions, updates, and deletions. However, the Changes API does support webhook notifications to avoid repeatedly polling the API endpoints.

This API endpoint especially helps with use cases involving basic one-way sync, such as with automation rules. However, the Drive Activity API described below is more helpful for use cases that require additional detail.

The Activity API

The Drive Activity API also retrieves changes in a user’s Google Drive or Team Drive but provides more detail than the Changes API does. File renames and moves don’t overlap with additions and deletions, for example. Here is an example DriveActivity object:

{
  "primaryActionDetail": {
    "create": {
      "new": {}
    }
  },
  "actors": [
    {
      "user": {
        "knownUser": {
          "personName": "people/105543818741636526322",
          "isCurrentUser": true
        }
      }
    }
  ],
  "actions": [
    {
      "detail": {
        "create": {
          "new": {}
        }
      }
    },
    {
      "detail": {
        "move": {
          "addedParents": [
            {
              "driveItem": {
                "name": "items/0AK48bYBbhWYoUk9PVA",
                "folder": {
                  "type": "MY_DRIVE_ROOT"
                }
              }
            }
          ]
        }
      }
    }
  ],
  "targets": [
    {
      "driveItem": {
        "name": "items/1sP77lasbdEw0MuzRYn_Wwe60fQsBtJ0e",
        "title": "test_75",
        "folder": {
          "type": "STANDARD_FOLDER"
        },
        "mimeType": "application/vnd.google-apps.folder",
        "owner": {
          "user": {
            "knownUser": {
              "personName": "people/105543818745636526322",
              "isCurrentUser": true
            }
          },
          "domain": {
            "name": "kloudless.com",
            "legacyId": "115685618164385049061"
          }
        }
      }
    }
  ],
  "timestamp": "2019-01-11T01:44:42.774Z"
}

Examples of additional detail include the type, target, and actor associated with the event. This enables nuanced responses to different types of activity, such as in use cases involving analysis and audit.

This API endpoint’s primary downside is that it requires activity to be queried rather than followed in a stream. The API response returns activity in descending order by time, which makes it harder to get new events beginning at a particular point in time. To do so, developers must paginate backward in time till reaching the point in the event stream they’d like to begin receiving data from.

The Reports API

Only G Suite admins can grant access to their organization’s activity data via the Admin SDK’s Reports API. It provides the ability to monitor all activity in a G Suite tenant.

This also means that all activity in Team Drives can be monitored with a single query, which is a major benefit over the Changes and Activity API that each require individual requests for activity on every Team Drive. For example, the Activity API only returns activity in a user’s individual Google Drive unless the request specifies a Team Drive ID as the ancestor.

To retrieve org-wide Google Drive activity using the Reports API, set the applicationName to drive in the list request‘s query parameter. Here is an example Drive Audit Activity event:

{
  "kind": "admin#reports#activity",
  "id": {
    "time": "2019-02-06T09:03:39.606Z",
    "uniqueQualifier": "-5443151506617447182",
    "applicationName": "drive",
    "customerId": "C01qa2s2v"
  },
  "etag": "\"S3VtFupSeWkpIL4X4oyC3FQHUIg/E2ZKSzeRrqE9s-FpaKGD2CoejFk\"",
  "actor": {
    "email": "jord@kloudless.com",
    "profileId": "100363596389311801930"
  },
  "ipAddress": "51.34.106.0",
  "events": [
    {
      "type": "access",
      "name": "delete",
      "parameters": [
        {
          "name": "primary_event",
          "boolValue": true
        },
        {
          "name": "billable",
          "boolValue": true
        },
        {
          "name": "doc_id",
          "value": "11Y1Z3vaZHkhWuOy69BqzNgXzpn-KyP8W"
        },
        {
          "name": "doc_type",
          "value": "unknown"
        },
        {
          "name": "doc_title",
          "value": "tést_file_53019599"
        },
        {
          "name": "visibility",
          "value": "private"
        },
        {
          "name": "owner",
          "value": "jord@kloudless.com"
        },
        {
          "name": "owner_is_team_drive",
          "boolValue": false
        }
      ]
    }
  ]
}

The Reports API especially helps with use cases related to audit and security, such as Data Loss Prevention. It also supports webhooks.

However, apps might find it challenging to identify complex actions since the API groups together several sub-events and marks one as the primary action. Certain inconsistencies may appear, such as both copies and new file creation including create as the primary action, and change_acl_editors representing either link creation or deletion. In some cases, the API doesn’t identify the primary event either.

API Google Drive Google (verb) teams

Published at DZone with permission of Jord Lin. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  • 3 Reasons Why VPs of Engineering Are Choosing Low-Code
  • Be a Better Team Player
  • The Rising Risks and Opportunities in API Security

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: