n8henrie / pycookiecheat

Borrow cookies from your browser's authenticated session for use in Python scripts.
https://n8henrie.com/2013/11/use-chromes-cookies-for-easier-downloading-with-python-requests/
MIT License
744 stars 111 forks source link

Two-factor authentication #37

Closed ru111 closed 3 years ago

ru111 commented 3 years ago

Is there a way to bypass an initial two-factor authentication ("you are logging in from a new browser, send an authentication code to email")? I am logged in on chrome but through python it registers as "new browser". Thank you for developing this by the way, it has made getting past the login page so easy.

I'm just using simple script to download a pdf at url which requires login. But unfortunately this downloads the page after the username & password login telling me that I need to authenticate using a code by either clicking on "Send me an email" or "Send me a text" button (which presumably directs you to a page with an input field for this code). If there is a miraculous way to get through this page it'd be grand but otherwise it seems understandable - I suppose it makes sense considering they use the "are you a robot?" checks sometimes as well.

cookies = chrome_cookies(url)
s = requests.Session()
r = s.get(url, cookies=cookies)
options = {'cookie': s.cookies.items(), 'javascript-delay': 1000}
pdf = pdfkit.from_url(url, "page.pdf", options=options)
n8henrie commented 3 years ago

Huh, good question. I assume that a combination of cookies and using the same user agent and source IP address should do the trick, but I've never tried.

What browser are you using? And what Python library for making the request?

ru111 commented 3 years ago

Is there a way to pass specific user agent and source IP? I'm using google chrome browser on a mac and the python library I'm using to download the page to pdf is pdfkit https://pypi.org/project/pdfkit/

n8henrie commented 3 years ago

Sorry, I hadn't seen the edit with the example code.

No, your source IP should be wherever you're making the request from, so if you tried to log in from home and then make a request from some remote server, it would probably flag that.

You'll need to look at the requests documentation for setting the user agent in requests, that's not a pycookiecheat thing.

ru111 commented 3 years ago

Thanks for the info, I'm actually using the same machine so the IP should be the same with browser & python access. I understand that this is not a pycookiecheat's issue, but thanks for pointing me to user agents because now I've passed my user agent in the request it gets the correct page. Thanks again!

n8henrie commented 3 years ago

That's awesome, I'm glad to hear it!

If the issue is resolved, would you mind closing?