20

I am fairly new to web programing but for the sake of it, I am trying to login to google account not using standard code but as a python application, but it is impossible to do so has anyone tried to this before? can anyone help?

2

4 Answers 4

28

I made a python class that handle google login and the is able to get any google service page that requires the user to be logged in:

class SessionGoogle: def __init__(self, url_login, url_auth, login, pwd): self.ses = requests.session() login_html = self.ses.get(url_login) soup_login = BeautifulSoup(login_html.content).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 

The idea is to go to the login page GALX hidden input value and send it back to google + login and password. It requires modules requests and beautifulSoup

Example of use:

url_login = "https://accounts.google.com/ServiceLogin" url_auth = "https://accounts.google.com/ServiceLoginAuth" session = SessionGoogle(url_login, url_auth, "myGoogleLogin", "myPassword") print session.get("http://plus.google.com") 

Hope this helps

4
  • Thanks! That's a really smart way to deal with all of the hidden elements, definetely gonna keep this 'trick' in my toolbox
    – MadRabbit
    CommentedNov 14, 2015 at 14:07
  • Super slick and makes my life way easier than playing with regex to get the GALX.
    – dreyco676
    CommentedAug 28, 2016 at 5:43
  • 1
    You could make a dictionary comprehension to make it prettier/simpler: my_dict = {i.get('name'): i.get('value') for i in lst if i.get('value')}CommentedJun 29, 2017 at 19:25
  • Does this still work, and if so, what are the url_login and url_auth to use?CommentedAug 9, 2018 at 2:49
1

Although probably not exactly what you were looking for here I found some code from a similar post that did run from me.

 import urllib2

def get_unread_msgs(user, passwd): auth_handler = urllib2.HTTPBasicAuthHandler() auth_handler.add_password( realm='New mail feed', uri='https://mail.google.com', user='%[email protected]' % user, passwd=passwd ) opener = urllib2.build_opener(auth_handler) urllib2.install_opener(opener) feed = urllib2.urlopen('https://mail.google.com/mail/feed/atom') return feed.read()

print get_unread_msgs("put-username-here","put-password-here")

reference:
How to auto log into gmail atom feed with Python?

    1

    2020 update for python 3:

    import urllib.request def unread_messages(user, passwd): auth_handler = urllib.request.HTTPBasicAuthHandler() auth_handler.add_password( realm='New mail feed', uri='https://mail.google.com', user='%[email protected]' % user, passwd=passwd ) opener = urllib.request.build_opener(auth_handler) urllib.request.install_opener(opener) feed = urllib.request.urlopen('https://mail.google.com/mail/feed/atom') return feed.read() print(unread_messages('username', 'password')) 
      0

      You can use urllib, urllib2 and cookielib libraries of python to login.

      import urllib, urllib2, cookielib def test_login(): username = '' # Gmail Address password = '' # Gmail Password cookie_jar = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar)) login_dict = urllib.urlencode({'username' : username, 'password' :password}) opener.open('https://accounts.google.com/ServiceLogin', login_dict) response = opener.open('https://plus.google.com/explore') print response.read() if __name__ == '__main__': test_login() 
      1
      • Is there a version of this that works in Python 3 (which does not use urllib2)?
        – toryan
        CommentedOct 1, 2019 at 0:03

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.