I am making a Python library to log into Google Accounts, but have been wondering if i can do that within python requests. I just wanted to fill out the spaces where it asks for your email. This is my code so far. I cant use Google APIs due to technical reasons on my Chromebook.
import requests
def submit_form(timeout=10, check_status_code=True):
try:
google_login = requests.get("https://accounts.google.com/ServiceLogin?elo=1", timeout=timeout, allow_redirects=True, verify=True)
if check_status_code:
print(google_login.status_code)
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
submit_form(timeout=10, check_status_code=True)
In general a form only exists within the browser.
The typical workflow for a user is that the user's browser will make a GET request for a page that has a form on it. This form will have some html associated with it that will tell the browser how to render each form item as well as where to submit the form (action) and what kind of data representation to use (application/x-www-form-urlencoded or multipart/form-data). The user does their thing and then eventually hits the submit button. At that point the browser transforms the all the form data and submits a POST request to the location specified with the data from the form.
As a python script, you don't need a browser, and if you already know details about the form, then you don't even need to GET the form. All you need to do is submit a POST request.
Now, if the website is friendly to automation it's possible their form is very consistent and easy to use. This effectively means that their form is an API specifying what data needs to be submitted.
However, some sites really don't want bots using their forms, so they change up their forms dynamically, so that trying to extract an API from them is difficult. Goggle does a LOT of stuff to make sure that it's really a human filling out it's forms in a browser rather than a bot. But also google makes it really easy for authorized bots to use a dedicated API.
So you should use their dedicated API, and I guarantee that if you can use python, you can use their API.