Skip to content

Instantly share code, notes, and snippets.

@valarpirai
Last active March 3, 2023 10:39
Show Gist options
  • Save valarpirai/61b1f43e034070f5f02f81fcb9f8b01e to your computer and use it in GitHub Desktop.
Save valarpirai/61b1f43e034070f5f02f81fcb9f8b01e to your computer and use it in GitHub Desktop.
const credentials = require('./credentials.json');
const googleAuth = require('google-oauth-jwt')
const config = {
"private_key": credentials.private_key,
"client_email": credentials.client_email
}
const getToken = () => {
return new Promise((resolve) => {
googleAuth.authenticate(
{
email: credentials.client_email,
key: config.private_key,
scopes: [
'https://www.googleapis.com/auth/dialogflow'],
},
(err, token) => {
console.log(err)
resolve(token);
})
})
}
console.log("token")
getToken().then((res) => {
console.log(res)
})
const credentials = require('./credentials.json');
const googleAuth = require('google-oauth-jwt')
const http = require("http");
const host = 'localhost';
const port = 8000;
const getToken = function() {
return new Promise((resolve, reject) => {
googleAuth.authenticate({
email: credentials.client_email,
key: credentials.private_key,
scopes: ['https://www.googleapis.com/auth/dialogflow'],
}, (err, token) => {
// console.log()
// console.log("token")
if (err != null)
reject(err)
else
resolve(token)
})
})
}
async function jwtToken(res) {
var token = await getToken()
if (res != null) {
res.end(JSON.stringify({token: token}));
}
return token
}
// console.log("got Token - " + jwtToken(null))
const requestListener = function (req, res) {
res.setHeader("Content-Type", "application/json");
switch (req.url) {
case "/token":
res.writeHead(200);
jwtToken(res)
// res.end(JSON.stringify({token: token}));
break
default:
res.writeHead(404);
res.end(JSON.stringify({error:"Resource not found"}));
}
}
// getToken().then((res) => {
// console.log(res)
// })
const server = http.createServer(requestListener);
server.listen(port, host, () => {
console.log(`Server is running on http://${host}:${port}`);
});
{
"name": "@paypalcorp/google_auth",
"version": "1.0.0",
"description": "",
"main": "google_api.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"google-oauth-jwt": "^0.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment