<?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: Yash Solanki</title>
    <description>The latest articles on DEV Community by Yash Solanki (@yksolanki9).</description>
    <link>https://dev.to/yksolanki9</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%2F243336%2F905326e7-81de-4845-bea0-ac6a378c07de.jpeg</url>
      <title>DEV Community: Yash Solanki</title>
      <link>https://dev.to/yksolanki9</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yksolanki9"/>
    <language>en</language>
    <item>
      <title>require vs import in NodeJs</title>
      <dc:creator>Yash Solanki</dc:creator>
      <pubDate>Fri, 07 Oct 2022 14:51:43 +0000</pubDate>
      <link>https://dev.to/yksolanki9/require-vs-import-in-nodejs-4fap</link>
      <guid>https://dev.to/yksolanki9/require-vs-import-in-nodejs-4fap</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you’re a NodeJs developer, you might have come across some code snippets which use &lt;code&gt;require()&lt;/code&gt; to import modules while others use &lt;code&gt;import&lt;/code&gt; for the same purpose; which might have got you to wonder, which one of these is correct and which should you use in your project? &lt;/p&gt;

&lt;p&gt;In this article, we’ll try to answer all such questions and clear the air around the difference between both the import techniques.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Scope&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;This article will walk you through the difference between the two import techniques in NodeJS, or, in general, the difference between the two module systems which use these import techniques.&lt;/li&gt;
&lt;li&gt;This article does not cover the difference in syntax for default, named, and mixed exports in both the module systems.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Modules and Module systems&lt;/strong&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Module systems enable us to structure and organize different modules in our Javascript code&lt;/li&gt;
&lt;li&gt;By default, Node uses CommonJS, while ES Modules is the Javascript official standard for writing reusable code on both client and server.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;As we are already aware, each file in Node is treated as a separate module. &lt;/p&gt;

&lt;p&gt;A module is a Javascript file containing related code. So as our application gets bigger, we can split it into different files viz. modules, which makes our code more readable and also allows us to reuse the code in different parts of our application&lt;/p&gt;

&lt;p&gt;The code inside these modules is not available globally i.e. you cannot directly access the variables, functions, and classes defined in one module in any other module.&lt;/p&gt;

&lt;p&gt;Instead, we have to explicitly tell NodeJS which parts of a module must be exported and which parts must be imported into a file. The &lt;strong&gt;module system&lt;/strong&gt; helps us to structure and organize these modules. &lt;/p&gt;

&lt;p&gt;In this article, we’ll try to understand two commonly used module systems - &lt;strong&gt;CommonJS and ECMAScript or ES Modules.&lt;/strong&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;CommonJS vs ES Modules&lt;/strong&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;require&lt;/code&gt; is used to import modules in CommonJS while &lt;code&gt;import&lt;/code&gt; is used in ES Modules.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Since &lt;code&gt;require&lt;/code&gt; and &lt;code&gt;import&lt;/code&gt; are just small parts of two different module systems, it makes more sense to differentiate between CommonJS and ES Modules to get a larger picture.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;CommonJS (Uses &lt;code&gt;require&lt;/code&gt; for importing modules)&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;CommonJS&lt;/code&gt; is the legacy module system implemented by NodeJS. In CommonJS, we can export modules using &lt;code&gt;module.exports&lt;/code&gt; and import them using &lt;code&gt;require&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;We can import various built-in modules, third party modules (installed via npm), or any of our local modules using &lt;code&gt;require&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//calculate.js&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;subtract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;subtract&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;//index.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;subtract&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./calculate&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;subract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;ES Modules (Uses &lt;code&gt;import&lt;/code&gt; for importing modules)&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;While Javascript did not have any built-in module system in the beginning, Node used CommonJS as its default module system&lt;/li&gt;
&lt;li&gt;But, in ES2015 (also commonly known as ES6), Javascript got its own module system - ES Modules, which has been widely adopted ever since, and NodeJs also added support for the same.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We can use ES Modules instead of the default CommonJS module system in Node by either&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Changing the extension of the file from &lt;code&gt;.js&lt;/code&gt; to &lt;code&gt;.mjs&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;So, in the example above, if we change the extension of the files to &lt;code&gt;.mjs&lt;/code&gt;, we can use the ES6 syntax for importing and exporting modules&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//calculate.mjs&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;subtract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;subtract&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;//index.mjs&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;subtract&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./calculate.mjs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;subract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Or instead of changing the file extension, we can also add a property &lt;code&gt;"type": "module"&lt;/code&gt; at the top-level of &lt;code&gt;package.json&lt;/code&gt; to use ES Modules across our Node application&lt;br&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"nodejs-app"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

    &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"module"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Difference between CommonJS and ES Modules
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;CommonJS modules can be imported at any place in the program while ES modules have to be imported only at the beginning of the program&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;express&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;require('express');&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="err"&gt;function&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;getData()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;data&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;require('./data');&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;//This&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;is&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;valid&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;//Do&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Something&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

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

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since CommonJS modules are imported dynamically at runtime, module bundlers like webpack cannot remove unused modules from the production build, thus increasing the size of the bundle&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CommonJs modules are loaded synchronously, which means that the modules are processed one by one, which can cause performance issues in large scale applications. ES Modules, on the other hand, are imported asynchronously, thus offering performance benefits as the application scales&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Widely used CommonJs variables like &lt;code&gt;__dirname&lt;/code&gt; , &lt;code&gt;__filename&lt;/code&gt; and &lt;code&gt;NODE_PATH&lt;/code&gt; ********are not available in ES Modules&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ES Modules is the official standard and most of the frontend frameworks like React, Vue etc use it, so it offers consistency in client and server side code&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Which one should you use?&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;If you want consistency in the backend and frontend, you should consider using ES Modules as it is the official standard for Javascript.&lt;/li&gt;
&lt;li&gt;Node v9 or below versions do not support ES modules. Though it’s very unlikely, but if you’re working on a project with these node versions, you won’t be able to use ES Modules&lt;/li&gt;
&lt;li&gt;In conclusion, it doesn’t matter if you’re using CommonJS or ES Modules as long as you’re confident with the choice.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Noob’s guide to using any Google API in NodeJs</title>
      <dc:creator>Yash Solanki</dc:creator>
      <pubDate>Sat, 18 Jun 2022 07:15:22 +0000</pubDate>
      <link>https://dev.to/yksolanki9/noobs-guide-to-using-any-google-api-in-nodejs-1j8g</link>
      <guid>https://dev.to/yksolanki9/noobs-guide-to-using-any-google-api-in-nodejs-1j8g</guid>
      <description>&lt;p&gt;Google offers multiple services to its users and also offers developers the option to build super cool stuff by integrating these services into their apps using their APIs. You can check the list of all the apis provided by Google &lt;a href="https://developers.google.com/apis-explorer" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article, I’ll walk you through the initial authentication step for using Google APIs. Lets jump right into it&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a project in Google Cloud Console
&lt;/h2&gt;

&lt;p&gt;To generate credentials for Google APIs, we need to create a project in Google Cloud Console. Sign in to Google Cloud Console with your google account and create a new project and follow the below steps:&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Enabling Google API
&lt;/h3&gt;

&lt;p&gt;In the sidemenu, select &lt;strong&gt;APIs and Services&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Library&lt;/strong&gt; -&amp;gt; Search for the Google API you want to use and enable it&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Google offers two basic types of credentials to access their API -  API key and oAuth 2&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;API key is used when accessing publicly available data like Google Maps&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;oAuth 2 is used when accessing private data like a user’s Gmail account or Google drive&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Getting oAuth credentials involves setting up an oAuth consent screen. You can skip this step if you’re looking only for API Key authentication&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Setting up the OAuth Screen
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Inside &lt;strong&gt;APIs and Services&lt;/strong&gt; -&amp;gt; &lt;strong&gt;OAuth Consent Screen&lt;/strong&gt; -&amp;gt; Select &lt;strong&gt;User Type&lt;/strong&gt;. You can  &lt;strong&gt;External&lt;/strong&gt; for testing&lt;/li&gt;
&lt;li&gt;On the next screen, enter all the application details&lt;/li&gt;
&lt;li&gt;In &lt;strong&gt;Scopes&lt;/strong&gt; section, click on &lt;strong&gt;Add Scopes&lt;/strong&gt;.  Each service offers different levels of permissions (i.e. scope) to client data. You can select the scope as per your project requirements.&lt;/li&gt;
&lt;li&gt;If your application is still in &lt;strong&gt;Testing&lt;/strong&gt; phase and you selected &lt;strong&gt;External&lt;/strong&gt; user type previous step, provide emails of all the users who can access your app&lt;/li&gt;
&lt;li&gt;Check the app summary and click &lt;strong&gt;Save&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 3: Generating Credentials
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;oAuth Client ID&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Again in &lt;strong&gt;APIs and Services&lt;/strong&gt;, open &lt;strong&gt;Credentials&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Create Credentials&lt;/strong&gt; -&amp;gt; &lt;strong&gt;oAuth Client ID&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Select your &lt;strong&gt;Application Type&lt;/strong&gt; and add

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Authorised Origin&lt;/strong&gt; - You can use localhost with port no. &lt;em&gt;&lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;&lt;/em&gt; if your app is not hosted&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Callback URL&lt;/strong&gt; - This is the url where Google will send the response after OAuth. Again for locally hosted apps, you can use something like &lt;em&gt;&lt;a href="http://localhost:3000/auth/google/callback" rel="noopener noreferrer"&gt;http://localhost:3000/auth/google/callback&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;API Key&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Generating API Key is fairly straight forward. Inside &lt;strong&gt;APIs and Services&lt;/strong&gt;, select &lt;strong&gt;Credentials&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Create Credentials&lt;/strong&gt; -&amp;gt; &lt;strong&gt;API Key&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Connecting our Node Project to Google API
&lt;/h2&gt;

&lt;p&gt;For connecting to Google API, we’ll use the Node client library - &lt;strong&gt;&lt;a href="https://www.npmjs.com/package/googleapis" rel="noopener noreferrer"&gt;googleapis&lt;/a&gt;.&lt;/strong&gt; Install it in your project using &lt;code&gt;npm i goolgeapis&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now to store the cloud project credentials in your app, install &lt;a href="https://www.npmjs.com/package/dotenv" rel="noopener noreferrer"&gt;dotenv&lt;/a&gt; and create a &lt;code&gt;.env&lt;/code&gt; file&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For oAuth2 authentication, add &lt;code&gt;CLIENT_ID&lt;/code&gt;, &lt;code&gt;CLIENT_SECRET&lt;/code&gt; and &lt;code&gt;CALLBACK_URL&lt;/code&gt; in the environment file.&lt;/li&gt;
&lt;li&gt;For API Key auth, just add the &lt;code&gt;API_KEY&lt;/code&gt; and skip the next step&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 1: Creating an oAuth2 client
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;In our app, we need to create a Node oAuth2 client using our generated credentials which validates our app as a valid client.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { google } = require('googleapis');
require('dotenv').config();
const { CLIENT_ID, CLIENT_SECRET, CALLBACK_URL } = process.env;

const oauth2Client = new google.auth.OAuth2(
  CLIENT_ID,
  CLIENT_SECRET,
  CALLBACK_URL
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In order to authenticate the users of our app and get consent to access their data, we need to generate a unique url based on our project scope. So this allows our app to make requests to Google apis on behalf of the user.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//This method returns a url where the users can see the oAuth consent screen
function getGoogleAuthURL() {

  // Add all the scopes required by your app in this array. 
  // For example, if the scope needed by your app is /auth/calendar, 
  // add it as https://www.googleapis.com/auth/calendar in the array
  const scope = [
    'https://www.googleapis.com/&amp;lt;scope-that-you-selected-earlier&amp;gt;'
  ];

  return oauth2Client.generateAuthUrl({
    //'offline' mode will return a refresh token which we can save in our database to access the user's data in the future
    access_type: 'offline', 
    scope,
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We can now create a separate route to redirect our users to the generated url
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/auth/google', (req, res) =&amp;gt; {
  res.redirect(getGoogleAuthURL());
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;When the user is redirected to the callback route, we can get the user details and save it in our DB and perform any other operation as needed
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function getGoogleUser({ code }) {
  // This will return an object with the access_token and refresh_token
  const { tokens } = await oauth2Client.getToken(code);
  oauth2Client.setCredentials({
    refresh_token: tokens.refresh_token
  });

  // Fetch the user's profile with the access token and bearer
  const googleUser = await axios
    .get(
      `https://www.googleapis.com/oauth2/v1/userinfo?alt=json&amp;amp;access_token=${tokens.access_token}`,
      {
        headers: {
          Authorization: `Bearer ${tokens.id_token}`,
        },
      },
    )
    .then(res =&amp;gt; {
      return { data: res.data, refresh_token: tokens.refresh_token };
    })
    .catch(error =&amp;gt; {
      throw new Error(error.message);
    });
    return googleUser;
}

app.get('/auth/google/callback', async (req, res) =&amp;gt; {
  try {
    const googleUser = await getGoogleUser(req.query);

    //Get user id, email and name from the response
    const { id, email, name } = googleUser.data;

    //You can store this refresh token in your db for future access
    const refreshToken = googleUser.refresh_token;

    //Store the data in DB and redirect to some other page

  } catch(err) {
    //Error handling logic here
  }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Authenticating API requests
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;We can authenticate all of our requests either at the global level or at a service level to avoid dealing with it in every request we send&lt;/li&gt;
&lt;li&gt;For API Key authentication, just replace the &lt;code&gt;oauth2Client&lt;/code&gt; with &lt;code&gt;API_KEY&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Global authentication
google.options({
  auth: oauth2Client
});

// Service level authentication - Example with Google Drive service
const drive = google.drive({
  version: 'v2',
  auth: oauth2Client
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And voila! Our Node app is now authenticated to use Google APIs. You can now easily GET and POST data to any of the Google apis provided you have defined the right scope.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9tkiw6a314m4rz1y1r7c.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9tkiw6a314m4rz1y1r7c.gif" alt="Dance" width="480" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can checkout endpoints for any of api in &lt;a href="https://developers.google.com/apis-explorer" rel="noopener noreferrer"&gt;Google Api explorer&lt;/a&gt; and refer the googleapis documentation &lt;a href="https://googleapis.dev/nodejs/googleapis/latest/index.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you! Do let me know in the comments if you have any questions.&lt;/p&gt;

&lt;p&gt;Stay healthy, Stay happy, Stay safe. Cheers!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
