A Guide to the Gmail API

Description:

The Gmail API tool allows users to call into specific Gmail account, using the gmailr package on RStudio to utilize various functions and tools. This tool gives users the ability to process large numbers of emails, compiling data into tables to quickly analyze the sender, subject line, body text, etc. Within these categories, other packages can be used to analyze for date, time, sentiment analysis, and word count. You can also attach email lists and use the gmailr package to send emails directly from the RStudio console.

Uses:

Email Collection

  1. Collection for further data analysis
  2. Filtering/sorting incoming messages
  3. Date and time when received
  4. Who the email is from
  5. Subject
  6. Body Text: Sentiment Analysis and Word Counts

Sending Emails

  1. Email Automation
  2. Utilize email lists
  3. Personalization with first and last names

Gmail API Setup

  1. Start by logging into your Google account at Google Cloud Console

2.Open up the projects in the top left and select New Project

  1. Once a New Project is selected and you pick a name, navigate to APIs and services .Search for the Gmail API, select, and Enable

  2. Next, proceed back to the cloud console and select OAuth Consent Screen. Create an app as external. When creating the app, keep naming consistent and use your email for all developer information. Adding a logo or domain is not necessary. Add any emails to test users that you would like to use for your API. Complete your app.

  3. Head to the Credentials tab on the left of your screen. Select “Create Credentials” and select “OAuth client ID” This will create a key and secret for use in RStudio. Application app is a Web Application and keep naming consistent. Under Authorized redirect URIs, Add URI and use the link http://localhost:1410/

  4. Once these credentials have been created, it will generate an ID and Secret, which will be used to copy into RStudio at a later time.

RStudio Setup

  1. Open a new R script and begin to make the call to the web hosted API.
  2. To begin, install package “gmailr”
  3. Next establish your key and secret. In order to make the process easier, establish the key and secret into their own values
key1 <- "XXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com"
secret1 <- "XXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
  1. Next, configure authorization by using the function gm_auth_configure(), and input the key and and secret
gm_auth_configure(key = key1, secret = secret1)
  1. To complete the authorization process, use function gm_auth(). This will take you into your web browser and will require the user to sign to one of the designated test accounts. You will be asked to consent to the app using your Google account to access and send emails. Once completed, head back to RStudio and you will be able to fully access your account.
# gm_auth()

RStudio Uses

Sending Emails:

Establish recipients, subject line, body, and attachments in the console

# tester <-
#  gm_mime() %>%
#  gm_to("asayj@xavier.edu") %>%
#  gm_from("pbont2019@gmail.com") %>%
#  gm_subject("Assignment 5") %>%
#  gm_text_body("Attached is my submission for Assignment 5")%>%
#  gm_attach_file("assignment5.Rmd")

# Verify it looks correct
# gm_create_draft(tester)

# Send draft directly from RStudio
# gm_send_message(tester)

Reading Emails:

Gain email ids and view directly in R, or export as .csv for later analysis

  #set filter
# email_filter <- 'label: College Mail)'
  #set number to collect
# num_emails_to_get <- 20
  #run gm_messages function with filter, number to collect you just set
# parker_emails <- gm_messages(search=email_filter, num_results = num_emails_to_get)
  #view what you've collected - list of message IDs and Thread IDs
# parker_emails
  #Centralize Message Ids
# ids = gmailr::gm_id(parker_emails, what="message_id")
  #use gm_message function to view data for single email
# msg<-gm_message("16a708aa08eef24e", user_id = "me", format = c("full", "metadata",
#                                                               "minimal", "raw"))