Search code examples
pythonbeautifulsouppython-requestspythonanywhere

Google login on Pythonanywhere


I'm using this code to connect remotely to my google account using requests. On local it works perfectly but when I'm trying to use on Pythonanywhere (same version of python 3.6 + I do have a paid account, a hacker plan) it doesn't work, it doesn't connect to my google account at all, without any error message in the console, do you have any idea what could be the problem ?

from bs4 import BeautifulSoup
import requests    

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

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")

Solution

  • You're never actually looking at the response of the login post request, so it's entirely likely that Google is rejecting your login for some reason and you have no way of knowing.