Instantly share code, notes, and snippets.

Embed
What would you like to do?
A demo of calling Google Calendar API
library(googleAuthR)
## set scopes for calendar
options(googleAuthR.scopes.selected = "https://www.googleapis.com/auth/calendar.readonly",
googleAuthR.client_id = "XXXX", ## add your Google project client Id
googleAuthR.client_secret = "XXXX") ## add your Google project client secret
## make sure calendar API is activated for your Google Project at below URL:
# https://console.cloud.google.com/apis/api/calendar-json.googleapis.com/overview
## this is taken and simplified from
# https://github.com/MarkEdmondson1234/autoGoogleAPI/blob/master/googlecalendarv3.auto/R/calendar_functions.R#L962
#' Gets a list of events for calendarId
#'
#' @param calendarId The calendar to get. Default is primary for authenticated user
#' @return a big list of JSON
events.list <- function(calendarId = "primary") {
url <- sprintf("https://www.googleapis.com/calendar/v3/calendars/%s/events",
calendarId)
f <- googleAuthR::gar_api_generator(url, "GET",
data_parse_function = function(x) x)
f()
}
## ---------
# To use:
## authenticate with email that has access to the calendar
gar_auth()
## should kick you out to Google OAuth2 flow. Come back here when done....
## get default (primary) calendar list
events <- events.list()
## events is raw JSON response,
## parse down to items by modifying the data_parse_function in events.list()
## or operating afterwards in code like below
events$items$summary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment