Log into Google account using Python?
<p>I want to Sign into my Google account using Python but when I print the html results it doesn't show my username. That's how I know it isn't logged in.</p>

<p>How do I sign into google using Python? I have seen two popular modules so far for this urllib.request or Requests but none have helped me with logging into the giant Google.</p>

<p>Code:</p>

<pre><code>import requests

# Fill in your details here to be posted to the login form.
payload = {
'Email': 'accountemail@gmail.com',
'Passwd': 'accountemailpassword'
}

# Use 'with' to ensure the session context is closed after use.
with requests.Session() as s:
p = s.post('https://accounts.google.com/signin/challenge/sl/password', data=payload)
# print the html returned or something more intelligent to see if it's a successful login page.
print(p.text)
</code></pre>

<p>Login form info:</p>

<pre><code>&lt;input id="Email" name="Email" placeholder="Enter your email" type="email" value="" spellcheck="false" autofocus=""&gt;

&lt;input id="Passwd" name="Passwd" type="password" placeholder="Password" class=""&gt;

&lt;input id="signIn" name="signIn" class="rc-button rc-button-submit" type="submit" value="Sign in"&gt;
</code></pre>

<p>When I login the console will give me 4 link to request so I'm not sure if I'm even using the right URL.</p>

<pre><code>Request URL:https://accounts.google.com/signin/challenge/sl/password
Request Method:POST
Status Code:302 

Request URL:https://accounts.google.com/CheckCookie?hl=en&amp;checkedDomains=youtube&amp;checkConnection=youtube%3A503%3A1&amp;pstMsg=1&amp;chtml=LoginDoneHtml&amp;service=youtube&amp;continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fhl%3Den%26feature%3Dsign_in_button%26app%3Ddesktop%26action_handle_signin%3Dtrue%26next%3D%252F&amp;gidl=CAASAggA
Request Method:GET
Status Code:302 

Request URL:https://accounts.google.com/CheckCookie?hl=en&amp;checkedDomains=youtube&amp;checkConnection=youtube%3A503%3A1&amp;pstMsg=1&amp;chtml=LoginDoneHtml&amp;service=youtube&amp;continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Fhl%3Den%26feature%3Dsign_in_button%26app%3Ddesktop%26action_handle_signin%3Dtrue%26next%3D%252F&amp;gidl=CAASAggA
Request Method:GET
Status Code:302 

request URL:https://www.youtube.com/signin?hl=en&amp;feature=sign_in_button&amp;app=desktop&amp;action_handle_signin=true&amp;next=%2F&amp;auth=xAMUT-baNWvXgWyGYfiQEoYLmGv4RL0ZTB-KgGa8uacdJeruODeKVoxZWwyfd-NezfxB6g.
Request Method:GET
Status Code:303 
</code></pre>

<p>I am currently using Python 3.4.2 &amp; don't plan on using google's API.</p>

<p>This will get you logged in:</p>

<pre><code>from bs4 import BeautifulSoup
import requests


form_data={'Email': 'you@gmail.com', 'Passwd': 'your_password'}
post = "https://accounts.google.com/signin/challenge/sl/password"

with requests.Session() as s:
    soup = BeautifulSoup(s.get("https://mail.google.com").text)
    for inp in soup.select("#gaia_loginform input[name]"):
        if inp["name"] not in form_data:
            form_data[inp["name"]] = inp["value"]
    s.post(post, form_data)
    html = s.get("https://mail.google.com/mail/u/0/#inbox").content
</code></pre>

<p>If you save and  open the html in a browser, you will see the <code>Loading you@gmail.comâ¦</code>, you would need Javascript to actually load the page. You can further verify by putting in a bad password, if you do you will see the html of the login page again.</p>

<p>You can see in your browser a lot more gets posted than you have provided, the values are contained in the <code>gaia_loginform</code>.</p>

<pre><code>&lt;form novalidate method="post" action="https://accounts.google.com/signin/challenge/sl/password" id="gaia_loginform"&gt;
  &lt;input name="Page" type="hidden" value="RememberedSignIn"&gt;
  &lt;input type="hidden" name="GALX" value="5r_aVZgnIGo"&gt;
  &lt;input type="hidden" name="gxf" value="AFoagUUk33ARYpIRJqwrADAIgtChEXMHUA:33244249"&gt;
  &lt;input type="hidden" id="_utf8" name="_utf8" value="&amp;#9731;"/&gt;
  &lt;input type="hidden" name="bgresponse" id="bgresponse" value="js_disabled"&gt;
  &lt;input type="hidden" id="pstMsg" name="pstMsg" value="0"&gt;
  &lt;input type="hidden" id="dnConn" name="dnConn" value=""&gt;
  &lt;input type="hidden" id="checkConnection" name="checkConnection" value=""&gt;
  &lt;input type="hidden" id="checkedDomains" name="checkedDomains"
         value="youtube"&gt;
</code></pre>

<p>I am obviously not going to share my email or password but you can I have my email stored in a variable <em>my_mail</em> below, you can see when we test for it that it is there:</p>

<pre><code>In [3]: from bs4 import BeautifulSoup

In [4]: import requests

In [5]: post = "https://accounts.google.com/signin/challenge/sl/password"

In [6]: with requests.Session() as s:
   ...:         soup = BeautifulSoup(s.get("https://accounts.google.com/ServiceLogin?elo=1").text, "html.parser")
   ...:         for inp in soup.select("#gaia_loginform input[name]"):
   ...:             if inp["name"] not in form_data:
   ...:                     form_data[inp["name"]] = inp["value"]
   ...:         s.post(post, form_data)
   ...:         

In [7]: my_mail in  s.get("https://mail.google.com/mail/u/0/#inbox").text
Out[7]: True
</code></pre>

python python-3.x