Introduction

This is a demonstration that chronicles my journey to link RStudio with Gmail. From the outset, I’ll admit that I did not think I could make this happen, at least, not with my Cochise College gmail account. That account is part of the college’s Google for Education plan (or whatever it’s called) and the security is screwed down pretty tight. I figured that I would never be able to use the Gmail API with my college account. However, I also have a personal gmail account and I figured at the very least I could use that account for this project.

I started with a Google search for “rproject gmail,” and I found a github link to gmailr (https://github.com/jimhester/gmailr), which is a gmail library for R. The Readme page for that project included a brief “setup” section. However, there was also a link to Send Email with R that had a more thorough setup process. I followed that process the best that I could (some of the steps in that Readme didn’t match the Gmail access I had), but in only about 30 minutes I had a Gmail link set up and sent a test message from my Cochise College gmail account to my personal gmail account. Here are the steps I followed.

Check my Gmail account

Using a suggestion from Jenny’s site, I verified that my college Google account is mail-capable by visiting https://mail.google.com/mail/ while logged in. It is, and that gave me a quick “win” with renewed hope that I could make this work out!

Create a Google Developer’s Console

  • I started Google Chrome and logged into my college Gmail account.

  • I went to https://console.developers.google.com/project and created my new project. I wasn’t at all sure that my College administrator would permit me to create a “developer’s” account. However, I was successful on this step.

  • I created a new project with the name RStudio-Gmail-Link. I originally tried to use GmailR but Google would not let me use that name since it could be confused with a Google product. I also discovered that project names are restricted to letters, numbers, quotes, hyphens, spaces or exclamation points (no underscores, which I tried at first).

  • The organization of cochise.edu was already filled in and I chose a location of cochise.edu and then clicked the Create button. The New Project screen is illustrated below.

    Create New Project at Google Developer

    Create New Project at Google Developer

  • I clicked the “hamburger” menu button at the top left corner and selected IAM & Admin -> IAM.

  • I made sure RStudio-Gmail-Link was in the app selection drop-down menu at the top left corner. This would only be important if I had more than one app at the site. The next image is the top-left corner of the IAM page. It shows the app selection drop-down menu link.

    The APP Selection Drop-down Menu

    The APP Selection Drop-down Menu

  • I clicked Identity-Aware Proxy in the left menu. That proxy required me to configure a Consent Screen so I clicked that button.

    The Identity-Aware Proxy Screen

    The Identity-Aware Proxy Screen

  • On the Credentials screen, I clicked the OAuth consent screen tab just under the Credentials header. On the consent screen I selected a Public type (which is the default) and then entered RStudio-Gmail-Link as the Application name. I accepted all of the other settings and clicked Save at the bottom of the screen. Note: I later went back and changed the Application type setting to Internal. I’m not sure that would change very much since a user must have the Client ID and secret to access the app, but I figured that any added layer of security would be good and I never intend to release this app to the general public anyway.

    The OAuth Consent Screen

    The OAuth Consent Screen

  • I clicked the Credentials tab under the Credentials header and then clicked Create Credentials. I selected OAuth client ID.

    The Create Credentials Screen

    The Create Credentials Screen

  • For Application type I selected Other and entered RStudio-Gmail-Link as the application name. Then I clicked the create button.

    Selecting the Application Type

    Selecting the Application Type

  • An OAuth client credential is created and a message pops up with the Client ID and Client Secret. However, there is a better way to get this information, so I just dismissed this box. Note, the ID and screte were partially obscured in the following illustration.

    The Client ID and Secret

    The Client ID and Secret

  • The credential that was just created is listed on the Credentials screen. Click the download button on the right side of the screen beside the name of the credential to download a JSON file containing the client ID and secret. That file will be used in RStudio to link a script to Gmail. Note: the Client ID and Client Secret information were partially obscured in the following illustration.

    The Credential Listing

    The Credential Listing

  • To link the Gmail API to this credential, click the library menu item in the left panel. A screen with dozens of potential APIs will pop up. Scroll down and click Gmail API.

    Selecting the Gmail API

    Selecting the Gmail API

  • Click to Enable this API.

    Enabling the Gmail API

    Enabling the Gmail API

  • A dashboard will pop up, but there won’t be any traffic to display yet. Note: the Create Credentials button can be ignored since credentials have already been created.

    The Initial Gmail API Dashboard

    The Initial Gmail API Dashboard

After I sent a couple of test messages with this API, I accessed the dashboard by clicking the IAM & Admin -> IAM link in the hamburger menu on the left side of the screen. It now shows all kinds of traffic!

The Gmail API Dashboard

The Gmail API Dashboard


  • Note: to delete an API project, click Manage Resources at the bottom of the menu on the left side of the IAM & admin page. Click the checkbox to the left of the resource and then click the Delete button at the top of the screen.

  • Note: to create a new API project, click Manage Resources at the bottom of the menu on the left side of the IAM & admin page. Click Create Project at the top of the screen.

Create RStudio Project

With a shiny, new Gmail API ready for service, it was time to try it out with an R script.

  • I created a project named RStudio-Gmail-Link in my online GitHub account and specified a .gitignore file that uses the R template.

  • I edited the .gitignore file and added this line: RStudio-Gmail-Link.json.

  • I cloned the repository to my local computer.

  • I opened RStudio and created a new project in the RStudio-Gmail-Link repository folder.

  • I renamed the JSON file that I downloaded from Google to RStudio-Gmail-Link.json and stored it in the RStudio-Gmail-Link repository folder.

First Script

Now, for my first Gmail script.

library(gmailr)

## My json credential file
use_secret_file("RStudio-Gmail-Link.json")

## My email message
test_email <- mime(
  To = "grself@gmail.com",
  From = "selfg@cochise.edu",
  Subject = "Gmail RStudio Link Test",
  body = "Can you hear me now?")

send_message(test_email)

The first time I ran the script it popped up an authorization screen where I verified with Google that it is OK for this app to access my gmail account. After a few seconds the email was sent from my Cochise College account to my personal gmail account.

Yea! Success!


Summary

With a link between my Gmail account and RStudio I can begin to explore other things that I may be able to do with this link. One of my first thoughts is to try to develop a personal network map for myself by downloading all of my email headers for some given month and then mapping the To addresses of people I emailed during that period.

Now things get fun…