<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ben Hammond</title>
    <description>The latest articles on DEV Community by Ben Hammond (@benhammondmusic).</description>
    <link>https://dev.to/benhammondmusic</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F578696%2Faf0389c7-fc6c-480e-98af-724f313d0ac5.jpeg</url>
      <title>DEV Community: Ben Hammond</title>
      <link>https://dev.to/benhammondmusic</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/benhammondmusic"/>
    <language>en</language>
    <item>
      <title>How To Connect Google Calendar API to Django on Heroku</title>
      <dc:creator>Ben Hammond</dc:creator>
      <pubDate>Thu, 24 Jun 2021 19:56:11 +0000</pubDate>
      <link>https://dev.to/benhammondmusic/how-to-connect-google-calendar-api-to-django-on-heroku-3834</link>
      <guid>https://dev.to/benhammondmusic/how-to-connect-google-calendar-api-to-django-on-heroku-3834</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Some tricky configuration is required, and the existing Python-specific documentation is sparse and buggy (or simply not written for a production setting). My eventual solution involves using a Google Cloud Platform service account and a custom Heroku buildpack&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://littlecabin.herokuapp.com" rel="noopener noreferrer"&gt;Little Cabin&lt;/a&gt; (my final project for my recently completed General Assembly bootcamp), provides tools for extended families to securely share their vacation property’s logistics and memories. One key feature is allowing the in-app scheduling to sync with a Google Calendar, and though I assumed it would be a simple matter of hooking up to the Google Calendar API, it was by far the most difficult problem to solve. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624483254791%2F89bl7Mbi7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624483254791%2F89bl7Mbi7.png" alt="Screenshot from the author's Little Cabin App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Project Setup
&lt;/h2&gt;

&lt;p&gt;To get myself started, I've followed along with my two previous blog posts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blog.benhammond.tech/django-getting-started" rel="noopener noreferrer"&gt;Setup a new Django app&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.benhammond.tech/deploying-django-to-heroku" rel="noopener noreferrer"&gt;Deploy it to Heroku&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;I have also created a &lt;a href="https://github.com/benhammondmusic/example-django-gcal-heroku" rel="noopener noreferrer"&gt;blog example repo on my GitHub&lt;/a&gt; that includes all of the steps in this entire tutorial, so feel free to clone that repo and have a look at the code. You'll still need to do a lot of person configuration to get it working, including creating your own service account, google calendar, Heroku project, .env files and Heroku config vars, etc. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Calendar Access Module
&lt;/h3&gt;

&lt;p&gt;We will start by creating a new module (just a separate  &lt;code&gt;.py&lt;/code&gt; file) to contain all of the Calendar API access methods. Python automatically &lt;em&gt;exports&lt;/em&gt;, but you must explicitly &lt;em&gt;import&lt;/em&gt; them into any code that needs access.&lt;/p&gt;

&lt;p&gt;In your &lt;code&gt;main_app/&lt;/code&gt; folder, create a file called &lt;code&gt;calendar_API.py&lt;/code&gt;.  In that blank file:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;touch main_app/calendar_API.py&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;then in VSCode, add the following to the blank file:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_calendar&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;RUNNING TEST_CALENDAR()&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;test_event1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;start&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;date&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;2022-01-01&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;end&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;date&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;2022-01-07&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;summary&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;test event 1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;test_event2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;start&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;date&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;2022-02-01&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;end&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;date&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;2022-02-07&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;summary&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;test event 2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;test_event1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;test_event2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Add Route to URLs
&lt;/h3&gt;

&lt;p&gt;In VSCode, open &lt;code&gt;main_app/urls.py&lt;/code&gt; and add the following the following item to the &lt;code&gt;urlpatterns[]&lt;/code&gt; array below the existing "home" route. This will give us two pages in total, our "Home" and our "Demo" which will demonstrate the connection to Google Calendar API&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;path('demo/', views.demo, name='demo'),&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Add To Views
&lt;/h3&gt;

&lt;p&gt;In your &lt;code&gt;views.py&lt;/code&gt;, import the module method at the top of the file using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;from .calendar_API import test_calendar&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now add a second method which will display the demo template (later on we'll fill in the code that actually does some work)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;demo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;test_calendar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;results&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;demo.html&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Add To Templates
&lt;/h3&gt;

&lt;p&gt;Create the html template that will serve as the HTML for eventually displaying the fetched results from the API:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;touch main_app/templates/demo.html&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Add the following to to the blank &lt;code&gt;demo.html&lt;/code&gt; file in VSCode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Demo&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
      {% for result in results %}
      &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;{{result.start.date}}{% if result.end.date %}-{% endif%}{{result.end.date}}: {{result.summary}}&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
      {% endfor %}
    &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, edit your &lt;code&gt;home.html&lt;/code&gt; to include a link to this new &lt;code&gt;demo.html&lt;/code&gt;. Add the following line after the &lt;code&gt;&amp;lt;h1&amp;gt;Home&amp;lt;/h1&amp;gt;&lt;/code&gt; line:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;a href="{% url 'demo' %}"&amp;gt;Connect to Google Calendar&amp;lt;/a&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Install Dependencies
&lt;/h3&gt;

&lt;p&gt;In your terminal, run the following commands to install needed packages and save those as project dependencies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pip3 freeze &amp;gt; requirements.txt&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  TEST IT!
&lt;/h2&gt;

&lt;p&gt;Let's make sure the plumbing is working before we add more logical complexity! &lt;/p&gt;

&lt;p&gt;In your terminal, start the local server (make sure you're in your activated &lt;code&gt;env&lt;/code&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;python3 manage.py runserver&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In your browser&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Visit &lt;a href="http://localhost:8000" rel="noopener noreferrer"&gt;localhost:8000&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Click on the "Connect to Google Calendar" link&lt;/li&gt;
&lt;li&gt;Confirm the headline "Demo" is displayed&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting up Calendar Access
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;These instructions draw heavily on &lt;a href="https://stackoverflow.com/questions/49480930/django-server-rw-access-to-self-owned-google-calendar" rel="noopener noreferrer"&gt;this amazing Stack Overflow answer&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Register for Google Service Account
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Follow Google's directions to setup the new credentials here: &lt;a href="https://developers.google.com/identity/protocols/oauth2/service-account" rel="noopener noreferrer"&gt;developers.google.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;You can skip the section entitled: "Delegating domain-wide authority to the service account"&lt;/li&gt;
&lt;li&gt;Save the credentials file somewhere memorable; you'll be using these locally and later when setting up Heroku config vars&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Configure a Test Calendar
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt; View your &lt;a href="https://calendar.google.com/calendar/r/month" rel="noopener noreferrer"&gt;Google Calendar&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;In the sidebar will be a list of your calendars; click the "+" button and select "Create New Calendar" from the dropdown&lt;/li&gt;
&lt;li&gt;Fill in a name "Example", and click the "Create Calendar" button&lt;/li&gt;
&lt;li&gt;Hover over this newly created calendar back in the sidebar, and click the three vertical dots, and select "Settings and Sharing" from the dropdown&lt;/li&gt;
&lt;li&gt;Scroll down to "share with specific people"&lt;/li&gt;
&lt;li&gt;Add your newly created service account email address (it will be "client_email" in your saved credentials file)&lt;/li&gt;
&lt;li&gt;Scroll down to "Integrate Calendar" and copy the Calendar ID string, then paste it somewhere safe as you'll need it again soon&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Manipulate Calendar from Local Django App
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Setup Local Config Vars
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Open your local environmental variables file; in this setup it's &lt;code&gt;example/.env&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;Add the following:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Google API
CAL_ID={{{THE CAL ID YOU COPY-PASTED FROM YOUR GOOGLE CALENDAR SETTINGS PAGE}}}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Create Your Local Credentials File
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;touch google-credentials.json&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Copy paste the entire JSON object from your downloaded credentials file into this newly created .json file. As an example, mine looks like this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "type": "service_account",
  "project_id": "example",
  "private_key_id": "BLAHBALH",
  "private_key": "-----BEGIN PRIVATE KEY-----\nSUUUUUUPER------LONG-------STRING\n-----END PRIVATE KEY-----\n",
  "client_email": "USERNAME@EXAMPLE.iam.gserviceaccount.com",
  "client_id": "1234567890987654321",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/username%40example.iam.gserviceaccount.com"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Update Your &lt;code&gt;.gitignore&lt;/code&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;in VSCode open your &lt;code&gt;.gitignore&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;Ensure all of the following lines are present, which will prevent git from indexing sensitive information (and later distributing it publicly on your GitHub)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# this hidden folder contains your local, virtual environment
.env

# this hidden file contains sensitive keys and environmental config vars
# if you've named your primary project folder something other than
# 'example' please adjust the following line as needed 
example/.env

# this token contains your sensitive google service account info
google-credentials.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Important!
&lt;/h3&gt;

&lt;p&gt;Please ensure both &lt;code&gt;example/.env&lt;/code&gt; and the newly created &lt;code&gt;google-credentials.json&lt;/code&gt; appear greyed out in VSCode&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624485007810%2Fu__Hg8BBu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624485007810%2Fu__Hg8BBu.png" alt="Screenshot showing greyed out gitignored files"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Add The API Calls
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Open &lt;code&gt;calendar_API.py&lt;/code&gt;, and replace the existing code with everything in the following block (basically replacing our test logging function with the real API calls)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from decouple import config
from google.oauth2 import service_account
import googleapiclient.discovery
import datetime

CAL_ID = config('CAL_ID')
SCOPES = ['https://www.googleapis.com/auth/calendar']
SERVICE_ACCOUNT_FILE = './google-credentials.json'


def test_calendar():
    print("RUNNING TEST_CALENDAR()")

    credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
    service = googleapiclient.discovery.build('calendar', 'v3', credentials=credentials)

    # CREATE A NEW EVENT
    new_event = {
    'summary': "Ben Hammond Tech's Super Awesome Event",
    'location': 'Denver, CO USA',
    'description': 'https://benhammond.tech',
    'start': {
        'date': f"{datetime.date.today()}",
        'timeZone': 'America/New_York',
    },
    'end': {
        'date': f"{datetime.date.today() + datetime.timedelta(days=3)}",
        'timeZone': 'America/New_York',
    },
    }
    service.events().insert(calendarId=CAL_ID, body=new_event).execute()
    print('Event created')

 # GET ALL EXISTING EVENTS
    events_result = service.events().list(calendarId=CAL_ID, maxResults=2500).execute()
    events = events_result.get('items', [])

    # LOG THEM ALL OUT IN DEV TOOLS CONSOLE
    for e in events:

        print(e)

    #uncomment the following lines to delete each existing item in the calendar
    #event_id = e['id']
        # service.events().delete(calendarId=CAL_ID, eventId=event_id).execute()


    return events
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Test It, Again!
&lt;/h2&gt;

&lt;p&gt;At this point, running the local server (make sure you're in your activated &lt;code&gt;env&lt;/code&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;python3 manage.py run server&lt;/code&gt; 
and viewing localhost:8000 in browser should display the same button as before. However, now clicking the button should trigger the series of API calls we added above:&lt;/li&gt;
&lt;li&gt;Create a new test event on your test calendar&lt;/li&gt;
&lt;li&gt;Download ALL events from that calendar (there's probably only the one unless you manually added any on the Google Calendar site)&lt;/li&gt;
&lt;li&gt;Display those downloaded events both as console logs and render them onto the HTML page under the "DEMO" heading&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If that's all working locally, the next big step is adjusting everything so that it works in production on Heroku.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connecting the Deployed App
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;These steps come mainly from &lt;a href="https://devdojo.com/bryanborge/adding-google-cloud-credentials-to-heroku" rel="noopener noreferrer"&gt;this excellent DevDojo.com blog post&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Add Custom Buildpack
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;In your browser, head to &lt;a href="https://dashboard.heroku.com/apps" rel="noopener noreferrer"&gt;Heroku&lt;/a&gt; and log in&lt;/li&gt;
&lt;li&gt;Open up the existing App that you've deployed (perhaps you followed my previous blog post: &lt;a href="https://blog.benhammond.tech/deploying-django-to-heroku" rel="noopener noreferrer"&gt;Deploying Django to Heroku&lt;/a&gt;?&lt;/li&gt;
&lt;li&gt;Choose "Settings" from the nav bar&lt;/li&gt;
&lt;li&gt;Scroll down and click the "Add Buildpack" button (there should already be "heroku/python" listed as installed&lt;/li&gt;
&lt;li&gt;Paste &lt;code&gt;https://github.com/buyersight/heroku-google-application-credentials-buildpack&lt;/code&gt; into the text input box labeled "Enter Buildpack URL"&lt;/li&gt;
&lt;li&gt;Click "Save changes"&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Add Heroku Config Vars
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Scroll up just a bit and click the "Reveal Config Vars" buttons&lt;/li&gt;
&lt;li&gt;You will now be adding all of the keys and corresponding values from your local &lt;code&gt;.env&lt;/code&gt; file, along with some addition Heroku only entries.&lt;/li&gt;
&lt;li&gt;In the KEY box, paste &lt;code&gt;CAL_ID&lt;/code&gt;, and in the corresponding VALUE box, paste the string that occurs after the &lt;code&gt;=&lt;/code&gt; in your local .env. In my case it looks like this &lt;code&gt;fF43grgewefwFWeww1231233r23rwq@group.calendar.google.com&lt;/code&gt;. Make sure there are no quotation marks or spaces before and after the string&lt;/li&gt;
&lt;li&gt;Click "Add"&lt;/li&gt;
&lt;li&gt;In the new blank KEY box, paste &lt;code&gt;GOOGLE_APPLICATION_CREDENTIALS&lt;/code&gt;, and in the corresponding VALUE box paste &lt;code&gt;google-credentials.json&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Click the "Add" button&lt;/li&gt;
&lt;li&gt;In the new KEY box, paste &lt;code&gt;GOOGLE_CREDENTIALS&lt;/code&gt;, and in the corresponding VALUE box, paste the entire JSON object. You can literally copy all of the code that is now in your local &lt;code&gt;google-credentials.json&lt;/code&gt;, starting with a &lt;code&gt;{&lt;/code&gt; and ending with a &lt;code&gt;}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Click the "Add" button&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624484649512%2FUXwecl10w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624484649512%2FUXwecl10w.png" alt="Screenshot showing what Heroku settings pages should look like with custom buildpack and config vars"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Deploy to Heroku
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Head back into your terminal, and save your progress in your local git:&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git add .&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git commit -m "getting ready for deploy"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Optionally push to your GitHub if you have added a remote repo&lt;/li&gt;
&lt;li&gt;Deploy using &lt;code&gt;git push heroku main&lt;/code&gt; (or &lt;code&gt;master&lt;/code&gt; if you haven't yet switched your local git to default to the &lt;code&gt;main&lt;/code&gt; branch)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Confirm Credentials Were Created
&lt;/h3&gt;

&lt;p&gt;Once you're notified the deploy was successful, you can use the terminal to manually explore your project's Heroku server and confirm that the credentials file was created automagically by the build pack and those config vars.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;heroku run bash&lt;/code&gt; (wait a few seconds for it to log in)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ls&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;You should see &lt;code&gt;google-credentials.json&lt;/code&gt; listed on the floor of your project. Yay!&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;exit&lt;/code&gt; will bring you back onto your local machine&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624486726987%2FAZixt1Yb7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624486726987%2FAZixt1Yb7.gif" alt="heroku run bash screen recording"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Run It!
&lt;/h2&gt;

&lt;p&gt;Head to your deployed Heroku site, and see if the button still works to populate / print out the Google Calendar events. If so, great job! If not, scope the last paragraph here for some potential debugging tricks. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As noted in the previous article &lt;a href="https://blog.benhammond.tech/deploying-django-to-heroku" rel="noopener noreferrer"&gt;Deploying Django to Heroku&lt;/a&gt;, it can be helpful to configure your Django project and Heroku to give more detailed error logging. Check out the instructions on &lt;a href="https://stackoverflow.com/questions/52311724/500-error-when-debug-false-with-heroku-and-django" rel="noopener noreferrer"&gt;this stackoverflow&lt;/a&gt; where they explain adding &lt;code&gt;DEBUG_PROPAGATE_EXCEPTIONS = True&lt;/code&gt; and a &lt;code&gt;LOGGING = { ... }&lt;/code&gt; library to their &lt;code&gt;settings.py&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Sundial Photo by &lt;a href="https://unsplash.com/@mkunsplash84?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Marian Kroell&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/sundial?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>django</category>
      <category>heroku</category>
      <category>python</category>
      <category>googlecloud</category>
    </item>
  </channel>
</rss>
