Skip to content

Python Access to Google Drive

kevinyu edited this page Nov 11, 2020 · 7 revisions

Easy way

Google Drive's web interface sucks and is terrible for uploading/downloading/browsing large datasets and files.

Install the latest version of our custom python package gdrive-access. Follow the link for installation and usage instructions.

pip install git+https://github.com/theunissenlab/[email protected]

Hard way

Here are specific instructions on getting google drive API credentials if you want to do things yourself or need more functionality than gdrive-access is giving you (although you can always just add what you need to gdrive-access instead!).

To do this, we will use pydrive, which lets you use python objects and functions. However, the way to get this set up is very annoying (setting up authentication). I will describe how to get this set up (tutorial good as of Jun 2019).

  1. Install pydrive (lets you make requests to google API through python)
  2. Create local settings and credentials files (so you do not have to repeatedly give access whenever you run your code)
  3. Create Google API Credentials (your "app" that will access your drive data)

1. Install pydrive

pip install pydrive

2. Create local settings and credentials files

In your project folder (where you will run your python code from), create the following files

client_secrets.json

credentials.json

settings.yaml

The first two should be empty. The third should look like this:

client_config_backend: settings
client_config:
  client_id:
  client_secret:
save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.json

get_refresh_token: True

(We will fill in the client_id and client_secret soon)

3. Set up Google API Credentials

The following instructions are copied from the pydrive documentation here: https://pythonhosted.org/PyDrive/quickstart.html

Its a little confusing, but at the end of this, you should have a client_secrets.json file in your project's directory, and have opened up a browser window asking for your permission to access your data.

When you download the client_secrets.json file, replace the empty file you created above, and copy the client_id and client_secret from that file into your settings.yaml file

Authentication


Drive API requires OAuth2.0 for authentication. PyDrive makes your life much easier by handling complex authentication steps for you.

  1. Go to APIs Console and make your own project.
  2. Search for 'Google Drive API', select the entry, and click 'Enable'.
  3. Select 'Credentials' from the left menu, click 'Create Credentials', select 'OAuth client ID'.
  4. Now, the product name and consent screen need to be set -> click 'Configure consent screen' and follow the instructions. Once finished:

a. Select 'Application type' to be Web application. b. Enter an appropriate name. c. Input http://localhost:8080 for 'Authorized JavaScript origins'. d. Input http://localhost:8080/ for 'Authorized redirect URIs'. e. Click 'Save'.

  1. Click 'Download JSON' on the right side of Client ID to download client_secret_.json.

The downloaded file has all authentication information of your application. Rename the file to "client_secrets.json" and place it in your working directory.

Create quickstart.py file and copy and paste the following code.

    from pydrive.auth import GoogleAuth
    
    gauth = GoogleAuth()
    gauth.LocalWebserverAuth() # Creates local webserver and auto handles authentication.

Almost done

Okay, so the above should have created a browser window, and the settings file should have made it so that your credentials are stored locally in credentials.json, so you do not have to repeatedly log in with the browser window anymore.

Now, in python you can create a drive object with

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

def get_auth():
  gauth = GoogleAuth()
  gauth.LocalWebserverAuth()
  return gauth

drive = GoogleDrive(get_auth())

Tada

Contents

General

Calendars and scheduling
Lab funds and purchases
Advising, Social Justice, Sexual Harassment, and Real World Shit * Support Resources

Dry lab

Getting connected to the lab network
Data storage and access
Computing
Working Remotely
Other Services

Wet lab

Animal Care

Husbandry, who to call, recordkeeping
Bird care links

Behavior

Pecking Test (NAF 125)
Field Station

Surgeries, Histology, Imaging

Protocols, "how to"s, techniques, and recipes
Instructions for individual pieces of equipment
Imaging

Electrophysiology

Instructions
Hardware, software, and techniques for ephys

Calcium imaging

* Ca imaging Notes

fMRI

Data Collection
Data Analysis

Theory

Modulations

STRFs

Other




Old pages:

Wetlab


Pages in progress:

Clone this wiki locally