Matthew Lawrence
Matthew Lawrence

Reputation: 3

How to pull a Google Workspace Professional users Drive utilization via API, for a single user and/or all users

I've been working in the field for a few years, but this is my first stack overflow question!

My organization would like to start tracking our drive utilization in Google Workspace/G-Suite by sending an email report every week with the total utilization.

I am having trouble finding a way to do this using either the google api's or google C# libraries. For simplicity I'll just describe problems with the API.

I can manage to pull the logged in user's drive utilization data using oauth, but cannot determine how to get a user's drive utilization for an account without logging in as that user, which defeats the purpose of generating a master report.

Google Workspace has a built in highlights report which displays the total storage used for the entire domain, but I can't find the underlying API calls that are used for that report. I've explored the google drive api's for some time and can't seem to find a way to do this programmatically.

The api call that seems the most likely to provide the result is https://www.googleapis.com/drive/v3/about?fields=storageQuota, user

But it only returns my accounts data. I've tried messing with the query parameters but can't seems to get any of our other users data. Here is an example of the returned data.

{
    "user": {
        "kind": "drive#user",
        "displayName": "My user name",
        "photoLink": "https://lh3.googleusercontent.com/a-/image-url",
        "me": true,
        "permissionId": "secret",
        "emailAddress": "[email protected]"
    },
    "storageQuota": {
        "usage": "468211110",
        "usageInDrive": "10651797",
        "usageInDriveTrash": "0"
    }
}

the fact that the "me" key in the json response seems to indicate that the api should support calling this for other users. Thanks in advance for your attention!

Upvotes: 0

Views: 70

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117254

You should consider using a service account. Once you have domain wide delegation set up its just a mater of specifying the user which you want to impersonate. You will need to run it once for each user.

string ApplicationName = "Gmail API .NET Quickstart";
            const string serviceAccount = "[email protected]";

            var certificate = new X509Certificate2(@"c:\test-api-ed4859a67674.p12", "notasecret", X509KeyStorageFlags.Exportable);

            var gsuiteUser = "[email protected]";

            var serviceAccountCredentialInitializer = new ServiceAccountCredential.Initializer(serviceAccount)
            {
                User = gsuiteUser,
                Scopes = new[] { GmailService.Scope.drive }

            }.FromCertificate(certificate);

Uploading files to Google Drive API with a service account

Upvotes: 0

Related Questions