<?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: fauzihazim</title>
    <description>The latest articles on DEV Community by fauzihazim (@fauzihazim).</description>
    <link>https://dev.to/fauzihazim</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%2F995336%2Fe1207bc0-b607-4a81-945e-8384b22e61bf.png</url>
      <title>DEV Community: fauzihazim</title>
      <link>https://dev.to/fauzihazim</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fauzihazim"/>
    <language>en</language>
    <item>
      <title>Google Oauth 2.0 in Node JS</title>
      <dc:creator>fauzihazim</dc:creator>
      <pubDate>Tue, 01 Jul 2025 07:32:24 +0000</pubDate>
      <link>https://dev.to/fauzihazim/google-oauth-20-in-node-js-5e2a</link>
      <guid>https://dev.to/fauzihazim/google-oauth-20-in-node-js-5e2a</guid>
      <description>&lt;p&gt;I write on Medium as a personal reference—to revisit my ideas whenever I’m working on a new project or building something. Here is how to integrate Google oauth 2.0 especially in Node JS.&lt;/p&gt;

&lt;p&gt;1 . Open &lt;a href="https://console.cloud.google.com/" rel="noopener noreferrer"&gt;https://console.cloud.google.com/&lt;/a&gt;&lt;br&gt;
2 . login to your Gmail&lt;br&gt;
3 . Click this&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%2Ftexko7t8n7yea5rkj4hk.png" 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%2Ftexko7t8n7yea5rkj4hk.png" alt="Google Oauth2.0 in Node JS" width="512" height="165"&gt;&lt;/a&gt;&lt;br&gt;
4 . And make new project by clicking this&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%2Fpm2jaudspjuur9mxp577.png" 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%2Fpm2jaudspjuur9mxp577.png" alt="Google Oauth2.0 in Node JS" width="512" height="165"&gt;&lt;/a&gt;&lt;br&gt;
5 . Input your project name and Location (Optional)&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%2Frk5n8dtg93891leurez3.png" 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%2Frk5n8dtg93891leurez3.png" alt="Google Oauth2.0 in Node JS" width="512" height="429"&gt;&lt;/a&gt;&lt;br&gt;
6 . Open the notification and select your project&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%2Fdv6l0r04es34iq85bt0t.png" 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%2Fdv6l0r04es34iq85bt0t.png" alt="Google Oauth2.0 in Node JS" width="800" height="209"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;7 . Click this to go to the API page&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%2Fg3jrmewr4fde7844zil5.png" 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%2Fg3jrmewr4fde7844zil5.png" alt="Google Oauth2.0 in Node JS" width="800" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;8 . On the Api &amp;amp; Services click “Enable APIs and services” button&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%2F3yc1h3vo0z2ydn09vqmj.png" 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%2F3yc1h3vo0z2ydn09vqmj.png" alt="Google Oauth2.0 in Node JS" width="781" height="277"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;9 . Click “Credentials” in the sidebar and click “Configure consent screen”&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%2F660d294xx4233hfu208q.png" 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%2F660d294xx4233hfu208q.png" alt="Google Oauth2.0 in Node JS" width="747" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;10 . After you click “Configure consent screen” then on this page click button “Get started”&lt;/p&gt;

&lt;p&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%2F6jcdrl2h9z45vxee0uah.png" 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%2F6jcdrl2h9z45vxee0uah.png" alt="Google Oauth2.0 in Node JS" width="730" height="304"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;11 . Input your “App name” and “User support email” and click “Next”&lt;/p&gt;

&lt;p&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%2Ffjv2l5fl2ouocpxeulp3.png" 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%2Ffjv2l5fl2ouocpxeulp3.png" alt="Google Oauth2.0 in Node JS" width="605" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;12 . Select “Internal” or “External” and click “Next”&lt;/p&gt;

&lt;p&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%2Fh2nvgdey9vffugm16pad.png" 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%2Fh2nvgdey9vffugm16pad.png" alt="Google Oauth2.0 in Node JS" width="778" height="556"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;13 . Input your email in “Contact Information” and click “Next”&lt;/p&gt;

&lt;p&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%2Fodgoymqkxp32qjwft7vj.png" 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%2Fodgoymqkxp32qjwft7vj.png" alt="Google Oauth2.0 in Node JS" width="731" height="589"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;14 . Checked the agreement and click “Continue”&lt;/p&gt;

&lt;p&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%2Fkwszbbo8330u8qa5cwt1.png" 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%2Fkwszbbo8330u8qa5cwt1.png" alt="Google Oauth2.0 in Node JS" width="655" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;15 . Click “Create” to create the project&lt;/p&gt;

&lt;p&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%2Fvl5r8crx4lthn4v3bt1c.png" 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%2Fvl5r8crx4lthn4v3bt1c.png" alt="Google Oauth2.0 in Node JS" width="465" height="537"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;16 . In Oauth Overview click “Create OAuth Client”&lt;/p&gt;

&lt;p&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%2Fuucz8l9ws99d8nfkbrfp.png" 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%2Fuucz8l9ws99d8nfkbrfp.png" alt="Google Oauth2.0 in Node JS" width="749" height="227"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;17 . In “Application Type” select “Web application”&lt;/p&gt;

&lt;p&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%2Fo3atucs6mx4t4gzio2ov.png" 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%2Fo3atucs6mx4t4gzio2ov.png" alt="Google Oauth2.0 in Node JS" width="549" height="229"&gt;&lt;/a&gt;&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%2Fq4f1ia4fpq59qgqotpkd.png" 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%2Fq4f1ia4fpq59qgqotpkd.png" alt="Google Oauth2.0 in Node JS" width="550" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;18 . Input “Name”, “Authorized Javascript Origin”, and “Authorized redirect URIs”. I personally use localhost:3000. Authorized redirect URIs used for url after login.&lt;/p&gt;

&lt;p&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%2Fi4bhi80pdsol5n7aqc2q.png" 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%2Fi4bhi80pdsol5n7aqc2q.png" alt="Google Oauth2.0 in Node JS" width="517" height="557"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;19 . It will show you the image, you will get client Id, client secret by click copy icon or download JSON&lt;/p&gt;

&lt;p&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%2Fbwi7h60mdtmmkg27mq19.png" 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%2Fbwi7h60mdtmmkg27mq19.png" alt="Google Oauth2.0 in Node JS" width="609" height="669"&gt;&lt;/a&gt;&lt;br&gt;
Copy that on .env file. Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;YOUR_CLIENT_ID = paste-your-Client-Id-here
YOUR_CLIENT_SECRET = paste-your-Client-Id-secret
YOUR_REDIRECT_URL = paste-your-redirect-URL-here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;20 . Click “Data Access” and “Add or remove scopes” to show Scope Option&lt;/p&gt;

&lt;p&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%2Fw3k9rlez1z8h019xo2ce.png" 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%2Fw3k9rlez1z8h019xo2ce.png" alt="Google Oauth2.0 in Node JS" width="603" height="247"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;21 . It will show a scope option. I personally select “.../auth/userinfo.email”, “.../auth/userinfo.profile”, and “openid”&lt;/p&gt;

&lt;p&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%2Fbbd21nexktw6uw4hwzle.png" 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%2Fbbd21nexktw6uw4hwzle.png" alt="Google Oauth2.0 in Node JS" width="645" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;22 . Click “Update”&lt;/p&gt;

&lt;p&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%2Fzueu5h8hr5u0j25ebpzz.png" 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%2Fzueu5h8hr5u0j25ebpzz.png" alt="Google Oauth2.0 in Node JS" width="636" height="250"&gt;&lt;/a&gt;&lt;br&gt;
23 . Execute “npm install googleapis crypto express express-session dotenv” in terminal project&lt;br&gt;
24 . Copy this in your project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {google} from 'googleapis';
import crypto from 'crypto';
import express from 'express';
import session from 'express-session';
import 'dotenv/config';

const app = express();


/**
 * To use OAuth2 authentication, we need access to a CLIENT_ID, CLIENT_SECRET, AND REDIRECT_URI
 * from the client_secret.json file. To get these credentials for your application, visit
 * https://console.cloud.google.com/apis/credentials.
 */
const oauth2Client = new google.auth.OAuth2(
  process.env.YOUR_CLIENT_ID,
  process.env.YOUR_CLIENT_SECRET,
  process.env.YOUR_REDIRECT_URL
);


// Access scopes for two non-Sign-In scopes: Read-only Drive activity and Google Calendar.
const scopes = [
    'https://www.googleapis.com/auth/userinfo.email',
    'https://www.googleapis.com/auth/userinfo.profile'
];


// Generate a secure random state value.
const state = crypto.randomBytes(32).toString('hex');


// Generate a url that asks permissions for the Drive activity and Google Calendar scope
const authorizationUrl = oauth2Client.generateAuthUrl({
  // 'online' (default) or 'offline' (gets refresh_token)
  access_type: 'offline',
  /** Pass in the scopes array defined above.
    * Alternatively, if only one scope is needed, you can pass a scope URL as a string */
  scope: scopes,
  // Enable incremental authorization. Recommended as a best practice.
  include_granted_scopes: true,
  // Include the state parameter to reduce the risk of CSRF attacks.
  state: state
});


app.get('/auth/google', (req, res) =&amp;gt; {
    res.redirect(authorizationUrl);
})


// Callback
app.get("/login", async (req, res) =&amp;gt; {     // Change login with your redirect url patch
    const {code} = req.query;
    const {tokens} = await oauth2Client.getToken(code);
    oauth2Client.setCredentials(tokens);
    const oauth2 = google.oauth2({
        auth: oauth2Client,
        version: 'v2'
    })
    const {data} = await oauth2.userinfo.get();
    res.send(data);
})

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

&lt;/div&gt;



&lt;p&gt;25 . I suggest using a newer version of googleapis or "googleapis": "^149.0.0" rather than using "googleapis": "^150.0.1". Because I faced error in &lt;strong&gt;node_module&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And make new project by clicking this&lt;/p&gt;

</description>
      <category>googleoauth</category>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
