Greeting fellowship of kings and queens.
I'm trying to implement user Google sign in using Google identity from https://developers.google.com/identity/sign-in/web at the front end and using the access token from
the front end to get GDrive file using Node.js Google API at the back end https://github.com/googleapis/google-api-nodejs-client/blob/main/samples/accessTokenAuth.js
Unfortunately, I am not able to get the result of the user GDrive sign-in file. Below is my code.
login.ejs file front end
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<script src="https://apis.google.com/js/platform.js" async defer></script>
<meta name="google-signin-client_id"
content="168395681600-uur7hvveutnmpn7a8265e0f1omlbogau.apps.googleusercontent.com">
</head>
<body>
<div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
<a href="/" onclick="signOut();">Sign out</a>
<script>
function onSignIn(googleUser) {
const access_token = googleUser.getAuthResponse(true).access_token
console.log(access_token)
var xhr = new XMLHttpRequest();
xhr.open('POST', '/login');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({ token: access_token }));
}
</script>
<script>
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
}
</script>
</body>
</html>
nodejs backend --server.js
const { google } = require('googleapis');
const express = require('express');
const app = express();
const port = process.env.PORT || 5000
/**
* This example demonstrates authenticating to the Google APIs backend using
* an access token only. Generally, access tokens are obtained using the standard JWT
* and 3-legged-OAuth methods, but from time to time only the access token
* is available.
*
*
*/
app.use(express.json())
app.set('view engine', 'ejs')
// app.get('/', (req, res) => {
// res.render('index')
// })
app.post('/login', (req, res) => {
let token = req.body.token
console.log(token)
const runSample = async(token)=> {
const drive = google.drive({
version: 'v2',
// this header will be present for every request
headers: {
Authorization: `Bearer ${token}`,
},
});
// Make an authorized request to list Drive files.
const res = await drive.files.list();
console.log(res.data);
return res;
}
if (module === require.main) {
runSample().catch(console.error);
}
runSample(token)
})
app.get('/login', (req, res) => {
res.render('login')
})
app.listen(port, () => { console.log(`listening to port:${port}`) })
package.json
{
"dependencies": {
"ejs": "^3.1.6",
"express": "^4.17.1",
"googleapis": "^89.0.0"
},
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js"
},
"name": "google-api-nodejs-client",
"version": "1.0.0",
"main": "index.js",
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"nodemon": "^2.0.14"
}
}
When I tried to check the scope by console.log(googelUser.getAuthResponse(true)) it only shows
scope:"email profile https://www.googleapis.com/auth/userinfo.profile openid https://www.googleapis.com/auth/userinfo.email"
But I already added all the scopes at the OAuth consent screen for this Google Drive API tq...