<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Scott McCartney on Medium]]></title>
        <description><![CDATA[Stories by Scott McCartney on Medium]]></description>
        <link>https://medium.com/@smccartney09?source=rss-76c2b67e46a4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xbglvqj5spNwWSj7pB38bg.jpeg</url>
            <title>Stories by Scott McCartney on Medium</title>
            <link>https://medium.com/@smccartney09?source=rss-76c2b67e46a4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 17:40:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@smccartney09/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Deploy a Spring Boot API to GCP App Engine]]></title>
            <link>https://medium.com/@smccartney09/deploy-a-spring-boot-api-to-gcp-app-engine-722198bab4d4?source=rss-76c2b67e46a4------2</link>
            <guid isPermaLink="false">https://medium.com/p/722198bab4d4</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[paas]]></category>
            <category><![CDATA[gcp]]></category>
            <category><![CDATA[app-engine]]></category>
            <category><![CDATA[deploy]]></category>
            <dc:creator><![CDATA[Scott McCartney]]></dc:creator>
            <pubDate>Fri, 19 Jun 2020 16:59:51 GMT</pubDate>
            <atom:updated>2020-06-19T16:59:51.828Z</atom:updated>
            <content:encoded><![CDATA[<p>You’ve been learning and developing with Spring Boot and Java, and feel ready to share what you’ve created with the world. You want to move beyond http://localhost:8080 and get your API out on the cloud for others to use. Learning infrastructure and cloud computing seems like a daunting task, but luckily in this day and age cloud providers like Google, Amazon, and Microsoft have made it as easier than ever to get your application out in the cloud.</p><p>The goal of this article will help get a simple version of a Spring Boot API out to Google’s App Engine so it can be accessed with a public URL.</p><p><strong>Prerequisites</strong>:</p><ul><li>A <a href="https://console.cloud.google.com/">GCP account</a> with billing enabled</li><li>gcloud <a href="https://cloud.google.com/sdk/gcloud">CLI tool</a></li><li><a href="https://www.oracle.com/java/technologies/javase-jdk11-downloads.html">JDK 11</a></li><li><a href="https://maven.apache.org/download.cgi">Maven CLI</a> (or an IDE with Maven)</li><li>Basic experience using a Terminal</li></ul><p>If you already have a Spring Boot API and a GCP project, feel free to skip to the “Add Configuration Files” section of this tutorial.</p><h3>What is GCP App Engine?</h3><p>Google Cloud Platform (GCP) is a cloud computing platform that offers a variety of different solutions for things like databases, security, and computing services without the need for users to setup their own data centers or servers. The two other largest cloud providers are AWS and Azure, but many other cloud solutions exist.</p><p>For computing services, Google has several options capable of running your applications. The Compute Engine is Google’s Infrastructure-as-a-Service (IaaS) product, giving you more control (but also more responsibility) over your application’s runtime environment. There is Google’s Kubernetes Engine (GKE) which allows you to deploy containers into clusters capable of scaling rapidly. Cloud Functions are GCP’s “serverless” solution that are capable of running code on demand, saving costs if the computation is simple and may be idle often. Other “-as-a-Service” solutions exist for computing and offer varying levels of configuration for the user.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/0*Qcg_KDCGgoL_S_DF.png" /><figcaption>IaaS vs PaaS vs SaaS (from <a href="http://www.sherweb.com/">www.sherweb.com</a>)</figcaption></figure><p>App Engine is GCP’s Platform-as-a-Service (PaaS). This means that Google will handle the underlying infrastructure (unlike Compute Engine). Instead, you can specify the runtime environment, deploy the application, and it just runs. Configuration is minimal, and you still get benefits from GCP like security, virtualization, scaling, and high reliability. Each cloud provider likely has their own PaaS solution similar to App Engine.</p><h3>Create a Spring Boot App</h3><p>If you do not already have a Spring Boot application made, Spring provides an easy UI to help get you started <a href="https://start.spring.io/">here</a>. The UI requires some metadata for your project and then allows you to add dependencies to help you implement features like database management, security, or building REST-ful APIs.</p><p><strong>IMPORTANT</strong>: Much of the metadata is arbitrary and up to you. For this tutorial though, the following properties will be necessary to use App Engine’s Standard environment:</p><ul><li>Packaging: Jar</li><li>Java: 11</li></ul><p>For dependencies, we’ll include the “Spring Web” dependency to build a REST API and “Spring Boot Actuator” to add a /actuator/health endpoint to check the app’s health status. When you’re done, your configuration should be similar to below.</p><figure><img alt="Example Spring Boot project configurations for this demo" src="https://cdn-images-1.medium.com/max/1024/1*MdWfAudIAgqixexpBSQiCg.png" /><figcaption>Spring Boot Project Configuration</figcaption></figure><p>Click “Generate” to download a .zip file of your Spring Boot project. Extract the folder and import it into your IDE of choice(I’ll be using <a href="https://spring.io/tools">STS</a>).</p><p><strong>Optional: </strong>We can rename our application.properties file in src/main/resources to application.yml . Then, paste the following configuration. When we deploy to App Engine later we will specify the production profile so that we have configurable environments in the cloud different than the one on our local computer, which is common.</p><pre>---<br>spring:<br>  profiles: default<br>message: &quot;Hello, from local!&quot;</pre><pre>---<br>spring:<br>  profiles: production<br>message: &quot;Hello, from App Engine!&quot;</pre><p>Next, let’s add an endpoint to our Spring Boot API. As mentioned before, the “Spring Boot Actuator” dependency gives us a health endpoint, but let’s also add our own. To do this, just create a new Java Class. In my case, I’ll name it HelloController.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/566/1*ZdOlTLX3f0BuQEYyERCwLQ.png" /><figcaption>Creating Java Class for a REST Controller</figcaption></figure><p>I specified this class in the “rest” package to help with organization but this isn’t necessary. Once created, you can copy the following code:</p><pre>import org.springframework.http.MediaType;<br>import org.springframework.http.ResponseEntity;<br>import org.springframework.web.bind.annotation.GetMapping;<br>import org.springframework.web.bind.annotation.RestController;</pre><pre>@RestController<br>public class HelloController {<br>   @Value(&quot;${message}&quot;)<br>   String message;</pre><pre>   @GetMapping(value = &quot;demo&quot;)<br>   public ResponseEntity&lt;String&gt; displayHelloMessage() {<br>      return ResponseEntity.ok(message);<br>   }<br>}</pre><p><strong>Note: </strong>The @Value annotation will use whatever value is in the application.yml file under “message” and store it in the String message; variable. When running the application, if a profile is not defined the “default” profile will be used and “Hello, from local!” will be stored in this variable. If you did not set up the application.yml in the optional section, remove the @Value(&quot;${message}&quot;) annotation and store whatever String you like in String message;.</p><p>The @RestController annotation at the class-level let’s Spring Boot know that this class will contain mappings for endpoints. The @GetMapping annotation actually declares an endpoint that we can hit. In this case, we’ll indicate that the endpoint is a GET request to /demo and will return a String response. A ResponseEntity is returned so that the HTTP Status Code can be specified. For this endpoint, we’ll return a 200 Status Cd of “OK”.</p><p>Once this class is added, start the application in order to test this new endpoint. You can either start it in your IDE or by running mvn spring-boot:run in the command line. Once running, navigate to localhost:8080/demo in your browser and confirm you get the following response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/319/1*_6Sbjp32wmUR5d-2DdRFsA.png" /><figcaption>The Response from the “/demo” Endpoint in our API</figcaption></figure><p>There you have it! You have an application ready and waiting to be packaged and deployed to GCP.</p><h3>Create a GCP Project</h3><p>With the application ready, we’ll need to create or select a GCP project that we’ll use to deploy to. This can be done through the CLI using gcloud projects create command, but I’ll use the online UI found <a href="https://console.cloud.google.com/home/dashboard">here</a>.</p><p>You can create a new project by first selecting the project dropdown in the top left and selecting “New Project”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/749/1*zqRPrd9VC_A11cQZRD4V6A.png" /><figcaption>Select “New Project” Option</figcaption></figure><p>On the next screen, specify the name of your GCP project. A Project ID will be generated based off that name. This Project ID is unique within all of GCP, so some numbers may be added in order to make it unique. Once you’ve filled in the form, it should look similar to below. If so, click “Create” to actually create the project within GCP.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/557/1*rTw8ELR3IiSK8HQrDC1_4w.png" /></figure><p>It may take a few minutes to actually create the project within GCP. Once created, navigate to the App Engine resource page via the navigation menu in the top left.</p><p><strong>NOTE</strong>: Adding App Engine to your project can also be done using the gcloud CLI tool by running gcloud app create in a Terminal if you’ve signed in with the gcloud auth login command and ran gcloud config set project PROJECT_ID using the PROJECT_ID listed above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JkI_XF35GsWviISMj8zEfQ.png" /><figcaption>App Engine Dashboard</figcaption></figure><p>On the App Engine Dashboard, you will have the option to click “Create Application” if you haven’t already provisioned App Engine in your GCP project. During the “Create Application” process, you will select the region to host your App Engine instance. You can also choose the runtime environment, which for the purposes of this tutorial should be “Java” and “Standard”.</p><p>Once the App Engine resource has been created, keep the page open and continue. During this, I noticed that closing the page may cause the App Engine resource to not be provisioned.</p><h3>Add Configuration Files</h3><p>With the App Engine instance ready, we can now add configuration file to our project. App Engine typically uses an app.yaml file under src/main/appengine in our project directory. Create this file and copy the following configuration:</p><pre>runtime: java11<br>env: standard<br>runtime_config:<br>  jdk: openjdk11</pre><pre>env_variables:<br>  SPRING_PROFILES_ACTIVE: &quot;production&quot;<br>handlers:<br>- url: /.*<br>  script: this field is required, but ignored<br>manual_scaling:<br>  instances: 1</pre><p>Some important notes in the configuration above:</p><ul><li>The “standard” environment is compatible with Java 11, but will be incompatible with Java 8 without further configurations</li><li>The “flex” environment only supports Java 8/9</li><li>Specifying the “production” Spring profile in the environment variables will automatically run the production configuration of our app as declared in src/main/resources/app.yml if created previously</li></ul><p>Some of the documentation in GCP may be misleading because Java 8 is still used as the default Java version. Because of that, most documentation will refer to “Java” when it really means “Java 8” specifically. This has led to GCP recommending the Flex environment for Java, or replacing the Tomcat server that Spring uses by default with a Jetty. Full documentation specifically for Java 11 in App Engine can be found <a href="https://cloud.google.com/appengine/docs/standard/java11">here</a>.</p><p>Using Java 11 and the “standard” App Engine environment allows us to deploy a Spring Boot application without any other changes to the configuration!</p><h3>Deploy to GCP</h3><p>We now have a Spring Boot API, the necessary configuration app.yaml file for our App Engine instance, and an App Engine resource provisioned in our GCP project. The only steps left are to package our Spring Boot app in a .jar file and deploy it!</p><p>To package the application in a .jar, you can either do it through your IDE using the “Run as &gt; Maven Build…” and including clean install as the goal, or through running mvn package in a Terminal window while in the root directory of your project. If run successfully, the output in the terminal window should look like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W82s6WJMy5uBIrbuAS-EBQ.png" /><figcaption>Terminal Output of “mvn package”</figcaption></figure><p>Maven will generate a .jar file under the target folder in our project, usually in the format of PROJECT-NAME-0.0.1-SNAPSHOT.jar. You may need to refresh the project directory and then expand the target folder. If viewing in your IDE, the result should look similar to the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*a_DLXFDWLhqCpizW6VaOHw.png" /><figcaption>Jar of Application in Project Directory</figcaption></figure><p>Copy the .jar file into the same folder as the app.yaml configuration file created in the last section, either by creating a new folder or moving the .jar into src/main/appengine. Those two files should be the only ones in the folder. Once done, navigate to that folder in your Terminal window.</p><p><strong>Note: </strong>To use the gcloud CLI tool, you may need to run gcloud auth login to login into your GCP account and then run gcloud config set project PROJECT_ID, replacing “PROJECT_ID” with the project ID of your GCP project. In my case, this would be appeng-deploy-demo-1.</p><p>With the GCP project set in your CLI, run gcloud app deploy to deploy your app! The process will take several minutes to complete, but the result should be similar to below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G7imSPIss32Q3UBcg0amFQ.png" /><figcaption>Result of Successful “gcloud app deploy”</figcaption></figure><p>The output of the successful process will include the endpoint of your deployed API. Copy that and be sure to add the “/demo” to hit our mapped endpoint. The output should match below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/930/1*FGDBfTgR3b7HQKduAi9uGw.png" /><figcaption>Hitting our Deployed API</figcaption></figure><p>And there you have it! As you can see, the “production” Spring profile in our app.yaml was configured correctly and the message was changed to say “Hello, from App Engine!”. This will allow us to configure other things like database connections differently than a local configuration. Your app is now up and running in GCP App Engine and publicly accessible!</p><p>Some notes:</p><ul><li>The deploy may fail if you do not have necessary permissions for the CLI to deploy. In that case, the logs will give you a link to the API to enable so that you can deploy from the CLI</li><li>If you navigated off the “Create application” screen when using the UI to create the App Engine resource, it may not complete the process of provisioning App Engine. Either recreate it with gcloud app create CLI command or use the “Create application” UI again</li><li>Your application will incur charges within GCP. You can disable the application easily under “Settings” in the App Engine Dashboard UI</li></ul><h3>Conclusion</h3><p>You now have a publicly accessible URL to hit your API, congrats! The next time you make a change and run gcloud app deploy, App Engine will automatically create a new version of your app and migrating all traffic to the new version. A CI/CD pipeline can be built so that anytime you update your code, the changes will be tested, built, and deployed automatically so that code is shipped reliabily and well-tested to App Engine.</p><p>Something I also found useful was adding the API under a custom subdomain of api.domainname.com by clicking “Settings” on the App Engine dashboard and selecting the “Custom Domains” tab. There, select “Add a custom domain”. When selecting the mappings, you can remove the domainname.com and <a href="http://www.domainname.com/">www.domainname.com</a> entries and add a mapping for api.domainname.com or whatever subdomain you’d like. This will generate a CNAME entry that you can add to your domain’s DNS settings, leading to consolidation of your API and a UI to a single domain name!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=722198bab4d4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Integrating Firebase Cloud Functions with Google Calendar API]]></title>
            <link>https://medium.com/zero-equals-false/integrating-firebase-cloud-functions-with-google-calendar-api-9a5ac042e869?source=rss-76c2b67e46a4------2</link>
            <guid isPermaLink="false">https://medium.com/p/9a5ac042e869</guid>
            <category><![CDATA[calendar]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[firebase-cloud-functions]]></category>
            <dc:creator><![CDATA[Scott McCartney]]></dc:creator>
            <pubDate>Wed, 31 Oct 2018 00:35:34 GMT</pubDate>
            <atom:updated>2020-01-15T00:06:23.801Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve recently started using Firebase for my projects, and found some success with their hosting, Firestore, and authentication services. When I tried using their Cloud Functions, I saw value in integrating it with their Google Calendar API. I wanted to be able to trigger the cloud function and have it manage my personal Google Calendar, but ran into some problems with the function’s authorization since I didn’t want to need a sign-in each time it executed.</p><p>After a lot of headaches, I consulted a fellow developer who had some experience with using OAuth2 and Google APIs. (Be sure to check out his Medium article on Node.js, OAuth2, and Gmail <a href="https://medium.com/@nickroach_50526/sending-emails-with-node-js-using-smtp-gmail-and-oauth2-316fe9c790a1">here</a>!) Be sure to have <a href="https://nodejs.org/en/">NPM </a>installed before beginning this tutorial.</p><p>With this problem solved, I was finally able to get my Firebase Cloud Function to quickly add events to my primary Google calendar via an HTTP trigger with only minimal details in the request body. A repository (minus the OAuth2 credentials.json file) with the code used to create our cloud function can be found <a href="https://github.com/scott-mccartney/google-calendar-cloud-function">here</a>.</p><h3>Part 1: Setting up your Firebase App</h3><p>Obviously, the first thing we need to is to create a Firebase app. This can be done through the <a href="https://console.cloud.google.com/">Google Developer Console</a> and integrating Firebase into your app later, but I’ll be using the <a href="https://console.firebase.google.com/">Firebase Console</a>. After signing in to your Google account, you should get a list of all your Firebase projects and an <strong>Add Project</strong> button. When you click <strong>Add Project</strong>, you’ll be prompted to fill in your project details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/696/1*MNmFZ5UI4P8QQBsn2AWXxg.png" /><figcaption>Creating a Firebase Project</figcaption></figure><p>The Project Name can be anything you like, but the Project ID must be unique through all Google projects. As you can see above, my project ID is <em>calendar-cloud-function, </em>but I’ll be using <em>PROJECT_ID</em> for the remainder of the article as an indication for you to replace with your specific Firebase project ID.</p><p>Be sure to accept all terms and conditions in the window. Once doing so, click <strong>Create project</strong> to finish creating your Firebase project. This process will take several seconds before confirming that your project was indeed created successfully. You can then enter your Firebase project.</p><blockquote>NOTE: Behind the scenes, a project is also made in the Google Developer Console that will be used later.</blockquote><p>If we go to the <strong>Functions</strong> tab on the left under <strong>Develop </strong>section, we’ll see our dashboard where our Cloud Functions will live.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_kO7gkl-fhs-hoi2o4EVkQ.png" /><figcaption>Firebase Cloud Functions Dashboard</figcaption></figure><p>Because we just created this project, there will be nothing listed here. Once you add a Function to your project, you can come back to this dashboard to see the logs, HTTP trigger, and other important information about your function.</p><h3>Part 2: Initializing our Firebase Project</h3><p>Now that we have a Firebase project initialized on our account, we’ll be able to add/deploy Cloud Functions and integrate any other Firebase product with our project.</p><p>Open up the command prompt and navigate to a workspace where you want to house your project. Run mkdir PROJECT_ID to create a directory with the same name as your Firebase project and cd into the new directory.</p><p>First, you’ll need to download the Firebase CLI with npm install -g firebase-tools. Once installed, login to your Firebase account with the command firebase login. This will grant the CLI access to your account so that you can manage and deploy code to specific Firebase projects.</p><p>The Firebase CLI also provides helpful tools to initialize code for specific Firebase products. Run firebase init to begin the setup the project architecture for your cloud function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/977/1*rSzkNoTLiPYO7q0miNUnjQ.png" /><figcaption>Initializing your Firebase project</figcaption></figure><p>For this tutorial, we are only working with Firebase Cloud Functions. Select <strong>Functions</strong> for your project setup and press enter to continue with the initialization of your project.</p><p>Next, the Firebase CLI will want to know which project you want your Cloud Function to be associated with. Select the correct <em>PROJECT_ID </em>for the Firebase project and press enter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1017/1*FxRoCXI48XZVwBYyQD7gog.png" /><figcaption>Selecting the Firebase Project</figcaption></figure><p>After pressing <strong>Enter</strong>, you will be given the option to write the Cloud Function in JavaScript or TypeScript. Feel free to choose either, but the code for this tutorial was written in JavaScript. Continue with the rest of the project setup. For ease of use later, be sure to install the npm dependencies and a linter during your project initialization.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*64PWpgTQ1IRBlYKUKgKsCw.png" /><figcaption>The Final Steps of Firebase Project Initialization</figcaption></figure><p>Once the npm dependencies are downloaded, your Firebase project should be created and ready to use. Open up the root directory in your favorite IDE and we can begin writing code!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/184/1*KDMUSoj48R-ATnghDsD6Ag.png" /><figcaption>Firebase Project Architecture</figcaption></figure><p>Your project should have the following architecture. If you enabled a linter for your project, we’ll need to make a minor change to the firebase.json file in the root directory.</p><p>In the predeploy array, edit the line &quot;npm --prefix \&quot;RESOURCE_DIR\&quot; run lint&quot; to &quot;npm --prefix functions run lint&quot;. The entirety the file should now be as follows:</p><pre>{<br>  &quot;functions&quot;: {<br>    &quot;predeploy&quot;: [<br>      &quot;npm --prefix functions run lint&quot;<br>    ]<br>  }<br>}</pre><p>In our index.js file, you should see some commented out code. At this point, let’s uncomment the helloWorld function and deploy it to Firebase in order to see if everything is configured correctly. After commenting out the function, index.js should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5VeKV7OOM-sLts2WLvkfKg.png" /><figcaption>A HelloWorld Cloud Function</figcaption></figure><p>This will return a message of “Hello from Firebase!” when the Cloud Function is executed. In the command prompt, run firebase deploy --only functions in the project’s root directory. The deploy will take ~1 minute to complete, but should succeed. Once successful, navigate back to your project’s Firebase dashboard and see our newly created helloWorld function listed in the <strong>Functions </strong>dashboard with an HTTP endpoint for a trigger.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QQzo7ihLBWRVz1eYJ9C6KQ.png" /><figcaption>Firebase Functions Dashboard</figcaption></figure><p>Copy the HTTP endpoint and paste it in a new tab of the browser. This will execute your function and you should get a “Hello from Firebase!” message back. This means that your project is configured correctly and now have a Cloud Function deployed!</p><p>This function doesn’t do much, but the opportunities for what it can do are endless. For this tutorial, we will be working with the Google Calendar API (v3). Google has <a href="https://console.cloud.google.com/apis/library">hundreds of other APIs available</a>, and I encourage you to check them out once you are done with this article. To work with Google APIs, you will first need to set up our OAuth2 credentials. This will authenticate your function when interacting with Google’s APIs.</p><h3>Part 3: Create Calendar API Credentials</h3><p>For this part, we’ll be generating our API credentials that our Cloud Function will use to interact with the Google Calendar API. This eliminates the need for sign-in prompts and allows your cloud function to execute quickly and reliably with a fresh token to access your specific Google Calendar.</p><p>First, go to the <a href="https://console.cloud.google.com/">Google Developer Console</a> and select the project you created if it is not already selected. You should be on your project’s main dashboard, with cards for many different aspects of your project. Select <strong>Go to APIs overview</strong> in the section for APIs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aHG6PMVSC3FBoiZfjLglYQ.png" /><figcaption>Main dashboard for your GCP Firebase project</figcaption></figure><p>In the API Dashboard, select <strong>Credentials</strong> from the menu on the left. If you’ve made credentials for other apps, you may have some already listed here, otherwise you’ll be prompted to create your own credentials.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TZrm6KZq_zh5klRVhcQH4w.png" /><figcaption>Options for Creating API Credentials</figcaption></figure><p>You may see a warning banner near the top of the page saying:</p><blockquote>To create an OAuth client ID, you must first set a product name on the consent screen</blockquote><p>To fix this, click the <strong>Configure consent screen</strong> button on the alert and pick a name for your “product”. Once you name your product, you may need to select an email for a <strong>Support Email</strong> before the Save button is enabled. Once enabled, click <strong>Save </strong>to be redirected to the OAuth Client ID creation screen.</p><p>On the credentials screen, select <strong>Web Application</strong> under <strong>Application Type</strong> to bring up the other criteria needed for OAuth credentials. Name the OAuth Client ID something meaningful so you can keep track of the credentials for your project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*MjWqNR0mVNX3t_AnTQz7UA.png" /><figcaption>OAuth Client ID criteria</figcaption></figure><p>Under <strong>Authorized redirect URIs</strong> in the <strong>Restrictions</strong> section, add the URI <a href="https://developers.google.com/oauthplayground"><strong>https://developers.google.com/oauthplayground</strong></a>. Once the redirect URI is added, click anywhere outside the input to ensure the URI was entered correctly. If everything looks correct, click the <strong>Create</strong> button at the bottom!</p><p>If your OAuth client credentials were created successfully, you will be redirected back to your credentials screen with the following alert window active:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pwZnMND0NUm-Mr7hjS-TaA.png" /><figcaption>Your OAuth Client credentials</figcaption></figure><p>If you click <strong>OK</strong>, you should see the credentials listed under the <strong>OAuth2.0 client IDs</strong> section on the Credentials page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a00jsS3ymbc6oKroas6BwQ.png" /></figure><p>In the right-most column for your client ID is a download icon. This will download a JSON file of the OAuth client credentials. The file should contain the following data:</p><pre>{  <br>   <strong>&quot;web&quot;</strong>:{  <br>      <strong>&quot;client_id&quot;</strong>:&quot;CLIENT_ID.apps.googleusercontent.com&quot;,<br>      <strong>&quot;project_id&quot;</strong>:&quot;PROJECT_ID&quot;,<br>      <strong>&quot;auth_uri&quot;</strong>:&quot;https://accounts.google.com/o/oauth2/auth&quot;,<br>      <strong>&quot;token_uri&quot;</strong>:&quot;https://www.googleapis.com/oauth2/v3/token&quot;,<br>      <strong>&quot;auth_provider_x509_cert_url&quot;</strong>: &quot;https://www.googleapis.com/oauth2/v1/certs&quot;,<br>      <strong>&quot;client_secret&quot;</strong>:&quot;CLIENT_SECRET&quot;,<br>      <strong>&quot;redirect_uris&quot;</strong>:[  <br>         &quot;https://developers.google.com/oauthplayground&quot;<br>      ]<br>   }<br>}</pre><p>Rename this file to credentials.json and save it in the same directory as index.js file for the project you created in Part 2.</p><h3>Part 4: Configure OAuth2 for your project</h3><p>Now that we have our OAuth Client credentials, we’ll need to actually go into our redirect URI and configure the OAuth2. To do this, go to <a href="https://developers.google.com/oauthplayground">https://developers.google.com/oauthplayground</a>. On the page, click the <strong>Settings</strong> icon in the top-right of the screen to pull up the configuration settings. Select <strong>Use your own OAuth credentials</strong> at the bottom to bring up inputs for the Client ID/Client Secret you just created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/509/1*Ha-DaAORzfRw-Qa5eXUZuA.png" /><figcaption>OAuth2 Configuration</figcaption></figure><p>After entering both the Client ID and Client Secret from your credentials.json file you created in Part 3, leave the above window open. On the left-side of the screen, search for the <strong>Calendar API v3 </strong>and select the appropriate scope that you want your cloud function to have.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*37GBhXEZWUWlCNBjzXAtpg.png" /><figcaption>OAuth2 Calendar API configuration</figcaption></figure><p>Because we want our app to manage/edit/read from your Google Calendar, select the scope https://www.googleapis.com/auth/calendar for full read/write access. Once that scope is selected, click <strong>Authorize APIs</strong> at the bottom of Step 1.</p><p>You’ll be taken to a Google sign-in page where you sign in to the Google account who’s calendar you want to access. Once signed in, you’ll be asked to confirm access for your credentials to manage your Google calendars. Approve of this message to continue. This will redirect you to <strong>Step 2</strong> of the OAuth2 Playground form.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/489/1*-j04uS9JeLutJC8gcDGU5A.png" /><figcaption>OAuth2 Configuration Step 2</figcaption></figure><p>You should have the <strong>Authorization code</strong> populated. This code is only valid for a short period of time (3600 seconds precisely). While it’s active, you’ll need to click <strong>Exchange authorization code for tokens</strong> to populate a refresh token that we will be able to use later in our cloud function.</p><p>For this tutorial, we will add this refresh token to the credentials.json file we created earlier to limit the number of files accessed by our cloud function later. An updated version is below, and your file should be the same format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w4daTRXCxVPYbiM4MoJrGA.png" /><figcaption>Updated credentials.json File</figcaption></figure><p>There you go! You now have OAuth2 configured for your cloud function so that the Refresh token, Client ID, Client Secret can get you a valid Access token when working with the Google Calendar API. If you initialize this project as a Git repository, be sure to specify credentials.json in your .gitignore file so that you don’t accidentally add your credentials to a public repository.</p><h3>Part 5: Write a Cloud Function to interact with Google Calendar</h3><p>Now that we have OAuth2 configured for the Google Calendar API, we can begin writing code to interact with it! To do this, navigate to your project in the command line and go into the functions folder where your package.json lives. There, run npm i googleapis --save to install the necessary Google API Node.js tools.</p><p>First thing we’ll do is import all the required packages we’ll be using. The completed code for index.js can be found <a href="https://github.com/scott-mccartney/google-calendar-cloud-function/blob/master/functions/index.js">here</a>. We’ll put this at the top of our file, before any methods we write later. Be sure that credentials.json is in the same folder as index.js, otherwise the path will be need to be updated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mu9juSn4tRYtNn3d1pKIwQ.png" /></figure><p>Next, we’ll begin writing our Firebase function. In Part 2 of this tutorial, we wrote a helloWorld() Firebase function. We’ll replace that with the following addEventToCalendar() method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GqOPIYPXjOYeXKLAaQnp1g.png" /></figure><p>The request and response parameters act as entry points and exits for our cloud function to interact with Firebase and the user. The request will have the data needed to make our calendar event, and we create that event in the first line of the function, storing it in eventData.</p><p>Next, we use our imported googleCredentials to create an oAuth2Client object with all the credentials we need to verify us as the correct user. We then set the refresh_token for the oAuth2Client credentials so that we have a fresh token when accessing the Google Calendar API.</p><p>Finally, we call a method addEvent() that has not been written yet, passing it the eventData and oAuth2Client details, waiting for either a <strong>Success </strong>or <strong>Failure</strong>. Once either is received, we pass the details of what happened back to the user with the response parameter given by Firebase.</p><p>The last method we need to write is the addEvent() method reference above. The full method is below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*72djCZxslhVC3jcosjaGWg.png" /></figure><p>As mentioned before, we passed our calendar event and authentication that were created in the initial addEventToCalendar() Firebase function. We then use the Google Calendar API by referencing the calendar object we created earlier. The full API for the Google Calendar can be found <a href="https://developers.google.com/calendar/v3/reference/">here</a>. If successful, we pass the newly-created calendar event data back in the Promise with a message of <strong>Success</strong> that will be handled by the Firebase function we created above.</p><p>And that’s it! The Firebase function is now complete. Like before, we will deploy this function by running the command firebase deploy --only functions in the root directory of the project. After a minute or two, the Firebase CLI will say that the Cloud Function is deployed.</p><h3>Part 6: Executing your Cloud Function</h3><p>Once your function is deployed, go to the <a href="https://console.cloud.google.com/">Google Developer Console</a> and navigate to your Firebase project. In the menu on the left, select <strong>Cloud Functions</strong> from the list. There, you should see the Firebase function you just created in your Cloud Functions dashboard. Click the addEventToCalendar function to get more information about it.</p><p>Here, you should see four tabs for your cloud function: <strong>General, Trigger, Source, </strong>and <strong>Testing</strong>. Check out each one to learn more about your function, including its HTTP trigger endpoint.</p><p>We’ll start by going to the <strong>Testing</strong> tab though. Here, you’ll see the following input options:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*EmJkSJ7B_2YDzxF3qX3RQw.png" /><figcaption>Testing Dashboard for the Cloud Function</figcaption></figure><p>The empty {} JSON object is your request body. If you remember, we access several elements of this request body in our cloud function to create the calendar event. Because of this, we’ll need to replace the Triggering Event to something along the lines of:</p><pre>{<br>  &quot;eventName&quot;: &quot;Firebase Event&quot;,<br>  &quot;description&quot;: &quot;This is a sample description&quot;,<br>  &quot;startTime&quot;: &quot;2018-12-01T10:00:00&quot;,<br>  &quot;endTime&quot;: &quot;2018-12-01T13:00:00&quot;<br>}</pre><p>Click <strong>Test the function</strong> to call your Firebase function with the above request body. If everything is correct, you should get a response with details about your newly created event. If you navigate to your Google Calendar, you’ll be able to see your event, exactly where it’s supposed to be.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/1*jTT5hnlr6xtopE1uBa_vEA.png" /><figcaption>Created Firebase Event</figcaption></figure><h3>Conclusion</h3><p>That’s it, we’ve officially got a Firebase Cloud Function deployed that will create events in your calendar with very minimal details needed in the request body. This function has an HTTP endpoint, so it can be hit by several different methods.</p><p>Further work could include using Alexa/Google Home to call that HTTP endpoint after adding specific voice prompts. Besides that, your interaction with the Calendar API could be extended by deleting events, modifying existing ones, reading out events occurring today, or others. Security for your Cloud Function could also be a next-step, so that you are sure that only you and your services are able to call your Cloud Function.</p><p>Now that you have a Cloud Function deployed in your Firebase project, I’d encourage you to look into several other Firebase services to expand your project even more. Good luck, and I hoped this help you learn something new about a cool, interesting technology!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9a5ac042e869" width="1" height="1" alt=""><hr><p><a href="https://medium.com/zero-equals-false/integrating-firebase-cloud-functions-with-google-calendar-api-9a5ac042e869">Integrating Firebase Cloud Functions with Google Calendar API</a> was originally published in <a href="https://medium.com/zero-equals-false">Zero Equals False</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>