M
M
MakarMS2021-08-08 01:52:18
Google
MakarMS, 2021-08-08 01:52:18

How to handle Google two-factor authentication?

How can I authenticate using Google two-factor in Python? I need to use requests to log in to my account on the site, but there is 2FA. Maybe there is some Google API or what?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-08-08
@MakarMS

Google's 2fa (and not only) is essentially just a secret string on the basis of which codes are generated. Install the pyotp library, get your secret string (usually, either the site gives it in its pure form, for use in other applications, or at least gives you the opportunity to generate a QR code that can be read by the camera and get a string from it; an alternative is to open the request logs in browser and generate a new 2fa code, it will be reflected somewhere in the history), then just do

import pyotp
code = pyotp.TOTP(secret_line).now()

And send this code via requests, just like a regular browser sends

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question