Skip to content

Instantly share code, notes, and snippets.

@smach
Forked from MarkEdmondson1234/google_calendar_demo.R
Last active October 2, 2017 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smach/ae92d7bb0d19f616406da8a8357280cf to your computer and use it in GitHub Desktop.
Save smach/ae92d7bb0d19f616406da8a8357280cf 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 - find it at https://console.developers.google.com/apis/credentials then click on the appropriate OAuth 2.0 client ID
googleAuthR.client_secret = "XXXX") ## add your Google project client secret - at same place as above
## 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