Skip to content

Instantly share code, notes, and snippets.

@MarkEdmondson1234
Last active September 12, 2024 19:22
Show Gist options
  • Save MarkEdmondson1234/0198e283cd228565f9313cf36f35c7ab to your computer and use it in GitHub Desktop.
Save MarkEdmondson1234/0198e283cd228565f9313cf36f35c7ab to your computer and use it in GitHub Desktop.
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