Find All Shared Files in Google Drive with a Python Script

Google Drive makes it very easy to share files. You can easily and securely share files to anyone with a Google account or to anyone else by creating a shareable link that is accessible to anyone that knows the link. The shareable link contains a 33 character random string, so it’s very difficult to guess the URL if you are worried about someone other than your intended recipient looking at the shared file.

Despite shareable links being very difficult to guess, it’s the simple fact that they are public that requires an audit every once in a while to see what files are shared and remove public access if necessary.

I don’t know why, but the Google Drive web interface does not make it easy to find all shared files. There have been plenty of requests for this feature on the Google Drive Help Forum over the years (1, 2, 3), but the feature still does not exist.

There are third-party options out there such as Drive Permissions Auditor or WhoHasAccess, but those options cost money and require trusting a third-party to access your Google Drive.

I wanted an easy to understand script completely controlled by me that shows all files I am currently sharing from Google Drive regardless if those files are shared to people with Google accounts or to anyone via a shareable link.

Because of Wesley Chun’s series of blog posts (1, 2), I was able to quickly piece together a Python script to list all files in my Google Drive and then make some modifications to show only the files that are shared.

The remainder of the post will walk you through everything needed to run the Python script.

Google Developer and Credential Setup

Go to the Google Developers Console and login with the Google account associated with your Google Drive.

You might be prompted to accept an updated Terms of Services

You will need to create a Project to enable the Google Drive API. If you don’t already have a Project, click the Create Project button and follow the instructions.

Once you have created a Project, enable the Google Drive API.

Next, you will need to create an OAuth client ID.

You will more than likely need to configure the OAuth consent screen first. Click the Configure consent screen button and fill out the Product name shown to users text field. Since you are the only person accessing your own private data, you can input whatever text string you want. Click Save when you are finished.

After clicking Save, you will be prompted to select an Application type. Select Other, input a Name such as google-drive-list-shared.py - which is the name of the Python script you will be running -, and click the Create button.

You will be presented with the OAuth client ID and client secret. Simply click OK.

Finally, you will need to download the JSON file containing the client ID and client secret. Under OAuth 2.0 client IDs you should see your newly created credential. To the far right click the down arrow icon. A JSON file will be downloaded to the Downloads folder on your workstation.

Rename the downloaded JSON file to client_secret.json.

Download the Python Script

Clone the git repository:

git clone https://github.com/jameswthorne/google-drive-list-shared.git

Change into the cloned repository:

cd ~/Downloads/google-drive-list-shared

Move the client_secret.json from earlier into this directory:

mv ~/Downloads/client_secret.json .

Create a Python virtual environment:

virtualenv env
source env/bin/activate

Install the Python requirements:

pip install -r requirements.txt

Run the Python Script

If you are running the Python script from your workstation, run the following command. Your web browser will open and require you to allow the Python script read access to your Google Drive:

python google-drive-list-shared.py

If you are running the Python script from a virtual machine without a GUI or web browser, run the following command instead and follow the prompted instructions:

python google-drive-list-shared.py --noauth_local_webserver

Once authenticated, the Python script will begin running and display only the files in your Google Drive that are shared to anyone with a Google account or to anyone else via a shareable link. The Python script might take a while to run if you have a lot of files saved in Google Drive.

References