Headless implementation of Google web login (with support for 2-Step Verification) in Python
Headless implementation of Google web login (with support for 2-Step Verification) in Python
py-google-auth
exposes a high-level Python module and REST API that can be used for headless login on Google Accounts. The API supports 2-step verification if it is enabled on Google Account being used.
Note: This project is in "alpha" version right now. We are actively developing it and expect it to be beta-ready in next couple of weeks.
MIT
The license text is available in LICENSE
file in root of this repo.
To install, run:
$ pip install py-google-auth
(for test):
$ pip install -i https://testpypi.python.org/pypi --extra-index-url https://pypi.python.org/pypi py-google-auth
to update the version:
$ pip install -Ui https://testpypi.python.org/pypi --extra-index-url https://pypi.python.org/pypi py-google-auth
To be able to make requests to API, you will need a token. You need to set it in your system environment for the API to access it and then pass it with every request you make:
export PY_GOOGLE_AUTH_TOKEN='some_token'
Also set a path for storing log files. These files will be created when ever some previously unhandled error will occur, in order to help debugging and fixing the problem. You can create a PR for such errors with the content of the file from your log path:
export PY_GOOGLE_AUTH_LOG_PATH=/path/to/logs/
Open your terminal and run:
py-google-auth
This will start a gunicorn server, which will listen on localhost:8001
by default. You can change host and port (run py-google-auth -h
for information).
Then you can make calls to the api using any HTTP library you like. The docs will contain examples with requests.
Example for an account without two factor auth enabled:
>>> import jsonpickle
>>> import os
>>> import requests
>>> token = os.environ.get('PY_GOOGLE_AUTH_TOKEN')
>>> data = {'email': 'myemail@example.com', 'password': 'myrandompassword', 'token': token}
>>> req = requests.post('http://localhost:8001/login', json=data)
>>> req
<Respose 200>
>>> session_str = req.json()['session']
>>> session = jsonpickle.decode(session_str)
>>> google_play_page = session.get('https://play.google.com/apps/publish')
>>> google_play_page
<Respose 200>
Note: jsonpickle
is used to encode python objects into json, since we get an encoded string which contains a request.Session object, we need to use decode to make it an object again.
More examples with other endpoints can be found in docs.
Normal login (without two factor auth).
POST /login --data {'email': email, 'password': password, 'token': token}
If two factor auth is enabled, then next request should go here:
POST /step_two_login --data {'session': session, 'method': method, 'otp': otp, 'token': token}
If you want to use alternate method for two factor, use this before /step_two_login
:
POST /change_method --data {'session': session, 'method': method, 'token': token}
Details about response data and status codes can be found in docs(writing soon).
We support following 'steps' (i.e. methods) offered by Google in 2-step verification:
We DONT support following 'step' (i.e. method):
We are in process of writing documentation, which will be hosted at http://py-google-auth.readthedocs.io/en/latest/.
To be done.