Created
March 15, 2020 19:42
-
-
Save shalvah/2d4b56fd124a44e4866f4da175e557d9 to your computer and use it in GitHub Desktop.
How to get a one-time access token from Google
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const TOKEN_PATH = 'token.json'; | |
const SCOPES = ['https://www.googleapis.com/auth/spreadsheets']; | |
const gapi = require('googleapis'); | |
const clientSecret = process.env.GOOGLE_CLIENT_SECRET; | |
const clientId = process.env.GOOGLE_CLIENT_ID; | |
const redirectUri = process.env.GOOGLE_REDIRECT_URI; | |
const oAuth2Client = new gapi.google.auth.OAuth2(clientId, clientSecret, redirectUri); | |
const fs = require('fs').promises; | |
function start() { | |
// Check if we have previously stored a token. | |
return fs.readFile(TOKEN_PATH, 'utf8') | |
.then(token => { | |
oAuth2Client.setCredentials(JSON.parse(token).tokens); | |
console.log("Already authorized."); | |
return; | |
}) | |
.catch(err => { | |
const authUrl = oAuth2Client.generateAuthUrl({ | |
access_type: 'offline', | |
scope: SCOPES, | |
}); | |
console.log('Authorize this app by visiting this url:', authUrl); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
}); | |
rl.question('Enter the code from that page here: ', (code) => { | |
rl.close(); | |
return oAuth2Client.getToken(code) | |
.then(token => { | |
oAuth2Client.setCredentials(token); | |
// Store the token to disk for later program executions | |
return fs.writeFile(TOKEN_PATH, JSON.stringify(token)) | |
}) | |
.then(() => { | |
console.log('Token stored to', TOKEN_PATH); | |
return console.log("Authorized.") | |
}) | |
.catch((err) => { | |
return console.error('Error while trying to retrieve access token', err); | |
}); | |
}); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const TOKEN_PATH = 'token.json'; | |
const SCOPES = ['https://www.googleapis.com/auth/spreadsheets']; | |
const gapi = require('googleapis'); | |
const clientSecret = process.env.GOOGLE_CLIENT_SECRET; | |
const clientId = process.env.GOOGLE_CLIENT_ID; | |
const redirectUri = process.env.GOOGLE_REDIRECT_URI; | |
const oAuth2Client = new gapi.google.auth.OAuth2(clientId, clientSecret, redirectUri); | |
const fs = require('fs').promises; | |
router.get('/auth/google/start', async (req, res, next) => { | |
// Check if we have previously stored a token. | |
return fs.readFile(TOKEN_PATH, 'utf8') | |
.then(token => { | |
oAuth2Client.setCredentials(JSON.parse(token).tokens); | |
return res.end("Already authorized."); | |
}) | |
.catch(err => { | |
const authUrl = oAuth2Client.generateAuthUrl({ | |
access_type: 'offline', | |
scope: SCOPES, | |
}); | |
return res.redirect(authUrl); | |
}); | |
}); | |
router.get('/auth/google/callback', async (req, res, next) => { | |
const code = req.query.code; | |
return oAuth2Client.getToken(code) | |
.then(token => { | |
oAuth2Client.setCredentials(token); | |
// Store the token to disk for later program executions | |
return fs.writeFile(TOKEN_PATH, JSON.stringify(token)) | |
}) | |
.then(() => { | |
console.log('Token stored to', TOKEN_PATH); | |
return res.end("Authorized.") | |
}) | |
.catch((err) => { | |
return console.error('Error while trying to retrieve access token', err); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment