0

I'm trying to login to Gmail by just using requests and then proceed to watch youtube, do some searches etc. I don't want to use selenium or any other alternative to selenium as I find it bulky and inconvenient

I was researching how to do this and I came across some answers here and based my code of that. However these solutions are from a couple years back and I don't know if it still applies now and if it will work to the purpose I want it to.

class SessionGoogle:
    def __init__(self, url_login, url_auth, login, pwd):
       self.ses = requests.session()
       login_html = self.ses.get(url_login)

       soup_login = soup(login_html.content, "lxml").find('form').find_all('input')
       my_dict = {}
       for u in soup_login:
           if u.has_attr('value'):
               my_dict[u['name']] = u['value']
       # override the inputs without login and pwd:
       my_dict['Email'] = login
       my_dict['Passwd'] = pwd
       self.ses.post(url_auth, data=my_dict)

   def get(self, URL):
       return self.ses.get(URL).text


   url_login = "https://accounts.google.com/ServiceLogin"
   url_auth = "https://accounts.google.com/ServiceLoginAuth"
   session = SessionGoogle(url_login, url_auth, "gmaillogint", "gmailpass")
   r = session.get("http://plus.google.com")  
   p = session.get("http://google.com")
   yt = session.get("https://youtube.com")

This code is taken from this post: Logging in to google using python?

The original post says that this can handle login on any google service page. Does this still apply today? If so can this be used to login to a Google (Gmail) account and watch youtube? Or will I have to use selenium?

Edit: I tried this code and it didn't result in any errors of any sort. However when I tried to print r.text or r.content I got a AttributeError: 'str' object has no attribute 'content'"error. This leads me to think that this is not working the way it should be.

Joell
  • 75
  • 1
  • 6

2 Answers2

1

That's not going to work. Google's login page has changed significantly since that post was written in 2014. It now includes some significant security features which are designed to prevent automated logins.

If you need to authenticate to a Google service, they probably have an API for that; refer to Using OAuth 2.0 to Access Google APIs for details. There is no API to "watch Youtube, do some searches, etc", though.

  • Yeah pretty much the reason I asked this question. Thanks for the answer. Guess its selenium time :( – Joell Dec 26 '18 at 03:43
  • 1
    Even that may not work. Google has put a _lot_ of effort into deterring automated logins. –  Dec 26 '18 at 06:59
1

I have another solution that might work for you and doesn't require any google API or so. Use Seleniumwire with undetected browser v2

Note: put chromedriver in your sys path.

from seleniumwire.undetected_chromedriver.v2 import Chrome, ChromeOptions
import time

options = {}
chrome_options = ChromeOptions()
chrome_options.add_argument('--user-data-dir=hash')
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--disable-dev-shm-usage")
# chrome_options.add_argument("--headless")
browser = Chrome(seleniumwire_options=options, options=chrome_options)

browser.get('https://gmail.com')
browser.find_element_by_xpath('//*[@id="identifierId"]').send_keys('your-email')
browser.find_element_by_xpath('//*[@id="identifierNext"]/div/button').click()
time.sleep(5)
browser.find_element_by_xpath('//*[@id="password"]/div[1]/div/div[1]/input').send_keys('you-password')
browser.find_element_by_xpath('//*[@id="passwordNext"]/div/button').click()
time.sleep(5)
browser.get('https://youtube.com')

In addition to this, selenium wire has many awesome features, check out Github repository

Anandesh Sharma
  • 436
  • 6
  • 22