Package: googleapiutils2
googleapiutils2Utilities forGoogle's v2 Python API.
Currently supports sections of the following resources:Drive:DriveResource,FilesResource,PermissionsResource,RepliesResource,...Sheets:SpreadsheetsResource,ValuesResource,...GeocodingQuickstart 🚀This project requires Python^3.10to run.Several dependencies are needed, namely the aforesaid Google Python API, but also
Google's oauth library, andrequests. Pre-bundled for ease of use are the fairly
monolithicgoogle-api-stubs, which greatly improves the usage experience.viapoetryInstall poetry, then runpoetry installAnd you're done.Overview 📖The library was written to be consistent with Google's own Python API - just a little
easier to use. MostDriveandSheetsoperations are supported using explicit
parameters. But most functions thereof take a**kwargsparameter (used for parameter
forwarding) to allow for the more granular usage of the underlying API.A note on IDs: anytime a resource ID is needed, one can be provide the actual resource
ID, or the URL to the resource. If a URL is provided, this mapping is cached for future
use.Authentication 🔑Before using aDriveorSheetsobject, one must first authenticate. This is done via
thegoogle.oauth2library, creating aCredentialsobject.Custom CredentialsThe library supports two methods of authentication:via a Google service account (recommended, see morehere)via OAuth2 (see morehere)With a service account, one can programmatically access resources without user input.
This is by far the easiest route, but requires a bit of setup.If one's not using a service account, the library will attempt to open a browser window
to authenticate using the provided credentials. This authentication is cached for future
usage (though it does expire on some interval) - so an valid token path is required.See theget_oauth2_credsfunction for more information.Default CredentialsTo expedite development, all credentials-based objects will default to using a service
account by way of the following discovery scheme:If./auth/credentials.jsonexists, use that credentials file.If theGOOGLE_API_CREDENTIALSenvironment variable is set, use the credentials
file pointed to by the variable.Drive 📁Example: copy a file to a folder.fromgoogleapiutils2importDrivedrive=Drive()filename="Heyy"file=drive.get(filename,parents=[FOLDER_URL])iffileisnotNone:drive.delete(file["id"])file=drive.copy(file_id=FILE_ID,to_filename=filename,to_folder_id=FOLDER_URL)What the above does is:Get the OAuth2 credentials using the default discvoery scheme (JSON object
representing the requisite credentials, seeherefor more information).create aDriveobject thereupon.Get the file with the given name, and delete it if it exists.Copy the file with the given ID to the given folder, and return the new file.Sheets 📊Example: update a range of cells in a sheet.sheets=Sheets()Sheet1=SheetsValueRange(sheets,SHEET_ID,sheet_name="Sheet1")rows=[{"Heyy":"99",}]Sheet1[2:3,...].update(rows)What the above does is:Get the OAuth2 credentials using the default discvoery scheme (JSON object
representing the requisite credentials, seeherefor more information).create aSheetsobject thereupon.Create aSheetsValueRangeobject, which is a wrapper around thespreadsheets.valuesAPI.Update the rangeSheet1!A2:B3with the given rows.Note the slicing syntax, which will feel quite familiar for any user of Numpy or Pandas.SheetSliceASheetsValueRangeobject can be sliced in a similar manner to that of a Numpy array.
The syntax is as follows:slc = Sheet[rows, cols]Whereinrowsandcolsare either integers, slices of integers (stride is not
supported), strings (in A1 notation), or ellipses (...).Note that Google's implementation of A1 notation is 1-indexed; 0 is invalid (e.g., 1
maps toA, 2 toB, etc.)ix=SheetSlice["Sheet1",1:3,2:4]#  "Sheet1!B2:D4"ix=SheetSlice["Sheet1","A1:B2"]#  "Sheet1!A1:B2"ix=SheetSlice[1:3,2:4]#  "Sheet1!B2:D4"ix=SheetSlice["A1:B2"]#  "Sheet1!A1:B2"ix=SheetSlice[...,1:3]#  "Sheet1!A1:Z3"values={SheetSlice["A1:B2"]:[["Heyy","99"],["Heyy","99"],],}# "Sheet1!A1:B2" = [["Heyy", "99"], ["Heyy", "99"]]ASheetSlicecan also be used as a key into aSheetsValueRange, or a dictionary (to
use in updating a sheet's range via.update(), for example). Further, aSheetsValueRangecan be sliced in a similar manner to that of aSheetSlice.Sheet1[2:3,...].update(rows)...Why "2" 🤔Don't ask :3