Trouble Logging In To Google with Headless Chrome / Puppeteer

别等时光非礼了梦想. 提交于 2019-12-04 14:06:57

Not possible I am afraid and not the answer you want.

I know I can't turn off 2-FA, so what would be the best way to bypass this?`

If it was possible to bypass then it kinda opens the door for hackers as Two-factor authentication works as an extra step in the process, a second security layer, that will reconfirm your identity. Its purpose is to make attackers' life harder and reduce fraud risks!

I would have added an Android app in the mix too. You can set up the 2FA with SMS codes and an Android app with SMS read permission can read the SMS and connect with a backend.

The backend can send push message, probably using Firebase Cloud Messaging to the local Node.js instance where the headless Chrome is running to input it in the 2FA screen.

I don't think there's any other way to do it. Although I would recommend not doing it, since it may open some backdoor for security issues.

I is actually possible using Twilio API within Puppeteer to programatically receive the SMS code. You will have to setup a special Google account for this to work with the Twilio number as mobile phone OR change your current Google account primary mobile number for the Twilio number, and use your regular number as a secondary contact in your Google account info.

My working solution (needs some refactoring)

const puppeteer = require('puppeteer');
(async () => {
  const browser = await puppeteer.launch({
    headless: false,        // for debugging only
    ignoreHTTPSErrors: true // This happens when you use a self signed certificate locally
  })
  const page = await browser.newPage()

  await page.setViewport({ width: 1280, height: 800 })
  await page.goto('https://myawesomesystem/loginFrm01')
  const navigationPromise = page.waitForNavigation()

  // Clicks on the login button    
  const googleLoginButtonSelector = 'body > section > ... > div'
  await page.waitForSelector( googleLoginButtonSelector )
  await page.click( googleLoginButtonSelector )

  // wait for the google oauth page to open
  const googleOAuthTarget = await browser.waitForTarget( target => {
    // console.log( target.url() ); // debugging
    return target.url().indexOf('https://accounts.google.com/signin/oauth/identifier') !== -1
  })

  const googleOAuthPage = await googleOAuthTarget.page()

  await googleOAuthPage.waitForSelector('#identifierId')
  await googleOAuthPage.type('#identifierId', CRED.user, { delay: 5 } )
  await googleOAuthPage.click('#identifierNext')

  await googleOAuthPage.waitForSelector('input[type="password"]', { visible: true })
  await googleOAuthPage.type('input[type="password"]', CRED.pass )

  await googleOAuthPage.waitForSelector('#passwordNext', { visible: true })
  await googleOAuthPage.click('#passwordNext')

  await navigationPromise

  // HERE:
  // the user has been authenticated
  // or login window was closed
  // or whatever else, please check

  await browser.close()
})()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!