A Clojure library that wraps the Google Java API for different Google Applications.
This library is partially typed using core.typed
. Run lein typed check
to type-check the library's code.
Latest test results (Thanks, CircleCI!):
If you're using Leiningen, just add this to your project.clj
:
[google-apps-clj "0.6.1"]
If you are using ClojureScript, there's a dependency conflict (see #22) between Google Closure Compiler and the Google Apps API library , so you'll have to add an exclusion, like so:
[google-apps-clj "0.6.1" :exclusions [com.google.guava/guava-jdk5]]
Check out CHANGELOG.md to see what's been updated lately.
The sheets v4 ns was substantially revised.
Major breaking changes were made to the Google Drive code in v0.5.0 which will require updating most of your code that interacts with Google Drive. Please see CHANGELOG.md for details.
- Google OAuth 2.0
- Google Drive
- Google Spreadsheets (v3 and v4)
- Google Calendar
In order to use any of these APIs, you must first use the Google OAuth 2.0 library to set up your credentials. All the APIs rely on the credentials received from setting up OAuth 2.0.
-
Log in to Google using the account you wish the credentials to be under
-
Navigate to the Developer's Console
-
Create a project and name it appropriately
-
On the project dashboard, find the "Use Google APIs" card and click the link to "Enable and manage APIs". On the left nav bar, select the credentials tab. You can also go directly to https://console.developers.google.com/apis/credentials?project=your-project-id (substituting in for
your-project-id
, of course). -
Navigate to Credentials and click Create new Client ID under OAuth. Choose "Installed application" and set up a consent screen if necessary.
-
Create a google-creds.edn file
-
Copy the Client ID, Client Secret, and Redirect URIs into your google-creds.edn. You will use the data in this file for getting the rest of your credententials and for the other APIs.
-
Read in your google-creds.edn file like so:
(edn/read-string (slurp "config/google-creds.edn"))
-
Call google-apps-clj.credentials/get-auth-map on this, along with the necessary OAuth scopes (eg
["https://www.googleapis.com/auth/drive" "https://docs.google.com/feeds/" "https://spreadsheets.google.com/feeds" "https://www.googleapis.com/auth/calendar"]
), and read in data and follow its instructions -
Copy the returned data into your google-creds.edn file under the
:auth-map
key. Reload it into your REPL. -
You are now ready to use the other APIs with your credential file
Google also supports Service Account credentials for server-to-server API access. The credential is provided as a JSON file containing a private key and some other data.
To obtain a service credential:
- Log in to Google using the account you wish the credentials to be under
- Navigate to the Developer's Console
- Create a project and name it appropriately
- Navigate to the API Manager, and enable the APIs you need an disable the default-provided ones you don't need
- Navigate to the Permissions > Service Accounts page
- Create a new service account, selecting the option to obtain a new private key as JSON.
- You should be given a JSON file containing the credential. You have a few options now:
- Store it anywhere, and set an environment variable (
GOOGLE_APPLICATION_CREDENTIALS
) to point to it - On Mac/Linux, rename and move the JSON file to be
~/.config/gcloud/application_default_credentials.json
(making directories as needed) - On Windows, rename and move the JSON file to be
%APPDATA%\gcloud\application_default_credentials.json
(making directories as needed)
Now you can use google-apps-clj.credentials/default-credential
to load these credentials, and then
pass them into most places that might otherwise expect a google-ctx
. For example:
(let [scopes [com.google.api.services.drive.DriveScopes/DRIVE]
creds (google-apps-clj.credentials/default-credential scopes)]
(google-apps-clj.google-drive/list-files! creds "FOLDERIDFOLDERIDFOLDERIDFOLD"))
(note in this scenario that the service account user has to be given the appropriate permissions in Drive.
The service account user won't show up by name in searches, but it can be added as a viewer/editor using its
[email protected]
email address as found in the JSON credential file)
- Searching
- Fetching
- Uploading
- Updating
- Deleting
- Authorizing
- Creating a Worksheet inside of an existing Spreadsheet
- Finding Spreadsheets by title and id, Worksheets by title and id, Cells by row column notation
- Editing a Worksheet's rows, columns, and title
- Editing individual cells in a Worksheet
- Editing rows in a worksheet
- Batch-updating large quantity of cells (improves performance)
- Reading an entire Worksheet's values or headers, or both
- Overwriting an entire Worksheet with a new set of data (destroys old data)
- Listing all events within a certain date-time range (for user)
- Listing all events on a given day (for user)
- Listing upcoming events with a supplied name (for user)
- Creating an event with a certain time range
- Creating an all day event
- Consider ditching the baroque Google java library in favor of direct integration with the api using clj-http or the like
- Consider making file maps the unit of work for the command fns
- Or possibly an Identifiable protocol to allow file ids or maps
- Consider a cleaner abstraction instead of a grab bag of fns
Copyright © SparkFund 2015-2017
Distributed under the Apache License, Version 2.0. See LICENSE.txt for details.