Authenticate to Google API with Node JS

Authenticate to Google API with Node JS

I've spent days trying to figure this out. I thought I found the answer&nbsp;<a href="https://stackoverflow.com/questions/43930184/get-access-token-on-server-side-javascript-nodejs-using-google-authorization-c" target="_blank" style="color: rgb(0, 89, 153);">HERE</a>, but I still can't get it working.

I've spent days trying to figure this out. I thought I found the answer HERE, but I still can't get it working.

Here's the relevant code:

var { google } = require('googleapis');
var http = require("http");
var request = require('request');

var oauth2Client = new google.auth.OAuth2( '<My Client ID>', '<My Client Secret>', 'http://localhost:8080' );

exports.generateAuthCodeUrl = function () {

const url = oauth2Client.generateAuthUrl({
    access_type: 'offline',
    scope: 'https://www.googleapis.com/auth/blogger'
});

return url;

};

exports.getAccessToken = function (accessCode) { var codeOptions = { code: accessCode } oauth2Client.getToken(codeOptions, function (err, tokens) { // Now tokens contains an access_token and an optional refresh_token. Save them. if (!err) { oauth2Client.setCredentials(tokens); return tokens; } console.log(err.message); }); };

What I have so far is the app redirects to the consent page. The user accepts, then I'm redirected back to localhost with a valid authorization code. From what I understand, I need to make another call and exchange this code for an access token. getAccessToken() is not working, however. The console log is returning this:

invalid_client
invalid_request

Please let me know which additional information is needed.

javascript node-js

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

How to Hire Node.js Developers And How Much Does It Cost?

A Guide to Hire Node.js Developers who can help you create fast and efficient web applications. Also, know how much does it cost to hire Node.js Developers.

Top 7 Most Popular Node.js Frameworks You Should Know

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser. In this post, you'll see top 7 of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).

Node.js for Beginners - Learn Node.js from Scratch (Step by Step)

Node.js for Beginners - Learn Node.js from Scratch (Step by Step) - Learn the basics of Node.js. This Node.js tutorial will guide you step by step so that you will learn basics and theory of every part. Learn to use Node.js like a professional. You’ll learn: Basic Of Node, Modules, NPM In Node, Event, Email, Uploading File, Advance Of Node.

Hands on with Node.Js Streams | Examples & Approach

The practical implications of having Streams in Node.js are vast. Nodejs Streams are a great way to handle data chunks and uncomplicate development.

Node.js Performance: Node.js vs. Io.js

You may already be aware that Raygun uses Node.JS for our API nodes that receive your precious crash reporting data (we also do node.js crash reporting if you’re interested). We’ve peaked in the past at more than 110,000 requests per second coming...