简体   繁体   中英

How to automate google 2-factor authentication using Puppeteer

(I want to automate a report download from Adwords. This report is not part of their API. So I thought of downloading it using browser automation. I'm using Puppeteer for browser automation.

But the problem is my account is 2-factor authentication enabled (I can't disable is due to security policy). And hence each time i will have to enter the OTP or backup codes. I tried adding the backup codes to an array and take one from it each time when required. But Google provides only 10 code at a time and hence it wont be fully automated.

Is any way I can fully automate the entire workflow?

Not sure it's still required, But as an alternative solution you can use python with selenium and pyotp(2 factor authentication ). Here I posted sample login + 2 factor auth. which can able to modify for the api as well.

import pyotp
from pyotp import TOTP
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome('./chromedriver_v80.exe') 
driver.get("https://www.example.com/")

wait = WebDriverWait(driver, 20)

login_username = driver.find_element_by_name("username")
login_username.clear()
login_username.send_keys(username)

login_passoword = driver.find_element_by_name("password")
login_passoword.clear()
login_passoword.send_keys(password)

driver.find_element_by_name("login").click()

# wait for the 2FA field to display
authField = wait.until(EC.presence_of_element_located((By.XPATH, "//*[@id='token']")))

# get the token from google authenticate
ga_secret = "4ythgotj3d784rjy6jw94o2xczpjgnta"   #your GA 32 character hash
totp = TOTP(ga_secret)
token = totp.now()
print(token)

# enter the token in the UI
authField.send_keys(token)
# click on the button to complete 2FA
driver.find_element_by_xpath("//*[@id='token_login']").click()

driver.close()

The workflow cannot be shared just like that in an answer. Here are some alternative resources that will help you achieve the desired result.

You are looking for theReporting API provided by google developers, it will let you download the reports as you wish. There is a rate limit set for this api. You will also need to read more about the OAuth refresh token .

在此处输入图像描述

Otherwise, If you want to automate the entire workflow using Puppeteer. I'd suggest automating the generation of OTP/Backup codes as part of workflow. That way you can have unlimited OTP codes available for you. Though it feels using their API would be the best choice.

If you have any related code, add them in your question and I'll be happy to update my answer accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM