Last active
January 16, 2024 19:04
-
-
Save howardbaik/e95ce3c836c1871b97341333f0bb2042 to your computer and use it in GitHub Desktop.
Extract list of attendees
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(httr2) | |
library(googlesheets4) | |
library(purrr) | |
# Function that creates OAuth client for Google APIs | |
google_client <- function() { | |
httr2::oauth_client( | |
id = "[Client ID]", | |
secret = "[Client secret]", | |
token_url = "https://oauth2.googleapis.com/token", | |
name = "howard-google-docs" | |
) | |
} | |
# Create a new HTTP request | |
open_agenda_url <- "https://docs.googleapis.com/v1/documents/1sPVsWeBZKn9A8sbfM666aRYwxVq5Emrhp0ws_R5X1n4" | |
req <- request(open_agenda_url) | |
# Authenticate and perform request | |
resp <- req %>% | |
req_oauth_auth_code( | |
client = google_client(), | |
auth_url = "https://accounts.google.com/o/oauth2/v2/auth", | |
scope = "https://www.googleapis.com/auth/documents" | |
) %>% | |
req_perform() | |
# Extract content from Google Doc | |
resp_body_json <- resp %>% resp_body_json() | |
content <- resp_body_json$body$content | |
# Iterate and look for list of attendees | |
result <- c() | |
for (ii in seq(1, length(content))) { | |
if (!is.null(content[[ii]]$paragraph$elements)) { | |
for (jj in seq(1, length(content[[ii]]$paragraph$elements))) { | |
if (!is.null(content[[ii]]$paragraph$elements[[jj]]$textRun$content) && | |
(content[[ii]]$paragraph$elements[[jj]]$textRun$content == "When you join the meeting, please enter your name and institution below:\n" | | |
content[[ii]]$paragraph$elements[[jj]]$textRun$content == "When you join the meeting, please enter your name and institution below:")) { | |
kk <- ii + 1 | |
while (!is.null(content[[kk]]$paragraph$elements[[1]]$textRun$content) && content[[kk]]$paragraph$elements[[1]]$textRun$content != "\n") { | |
result <- c(result, content[[kk]]$paragraph$elements[[1]]$textRun$content) | |
kk <- kk + 1 | |
} | |
} | |
} | |
} else { | |
next | |
} | |
} | |
# Post-processing | |
result <- gsub("\n$", "", result) | |
# Final result | |
result |
In Google Cloud, when creating the "OAuth consent screen", make sure to add your email address to "Test users" so you don't get blocked by Google from authentication. According to Google, "OAuth access is restricted to the test users listed on your OAuth consent screen".
Fetch Client ID and Client Secret from Google Cloud
- Create project on Google Cloud
- Enable Google Docs API
- Configure OAuth consent screen
Fill in the blanks as follows:
User Type: "External"
App name: "itn-metric"
User support email: [email protected]
Developer contact info: [email protected]
ADD SCOPES: Check Scope `../auth/documents`
IMPORTANT: Add test users: Your Google email address that you will use for this package.
- Create Credentials -> Create OAuth client ID -> Application Type: Desktop app
- Copy and paste Client ID and Client secret!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Obviously, you have to supply your Client ID and Client Secret from your Google Cloud account. Read this vignette for more info: https://httr2.r-lib.org/articles/oauth.html