I tried to access access_token from oauth 2.0 login by using Express and puppeteer.
var express = require('express');
const puppeteer = require('puppeteer');
var app = express();
app.get('/', function(req, res) {
run().then(() => console.log('Done')).catch(error => console.log(error));
async function run(){
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto('https://abcd.com/authorize?
audience=https://abcd.com&scope=openid%20email%20profile&client_id=abcd&response_type=token&redirect_uri=https://abcd.com/callback');
await new Promise(resolve => setTimeout(resolve, 5000));
await page.focus('#email');
await page.keyboard.type('[email protected]');
await page.focus('#password');
await page.keyboard.type('efghi');
const waitForLoad = new Promise(resolve => page.on('load', () => resolve()));
await page.evaluate(() => {
document.querySelector('span[class="label"]').click();
});
await waitForLoad;
console.log('Waiting to be redirected to the client.');
const clientUrl = await page.evaluate(() => window.location.href);
//1st the split is from when it encounters = in the url
var split1 = clientUrl.split('=');
//2nd split is when it encounters & in the 2nd object of the array
var split2 = split1[1].split('&');
//taking array in an object and conversing it to json
var obj = {
access_token: split2[0]
}
await browser.close();
res.send(obj);
};
});
app.listen(8000, function(){
console.log('Heard on 8000');
});
This can be run on postman to run other api with the received access token.