Convenience utility for implementing Google login with Flask.
Project description
Simple Flask Google Login
Written by Alvin Wan for educational purposes. See the accompanying tutorial on How third-party login works.
Convenience utility for implementing Google login with Flask.
This is more useful as a template than as an installable library. Either way, it's an easy way to get up and running with Google login without much work.
Get Started
Install the utility with pip.
pip install simple-flask-google-login
Initialize the utility's manager with the Flask app.
from simple_flask_google_login import SimpleFlaskGoogleLogin
SimpleFlaskGoogleLogin(app)
Alternatively, initialize with the app after construction.
manager = SimpleFlaskGoogleLogin()
manager.init_app(app)
Note that you need 4 prerequisites in order for this to work:
- Client Credentials: This application by default expects
client_secrets.json
to be in the same directory as the application. This is the file downloaded from the Google developers console. You can change this by passing theclient_secrets_path
argument to theSimpleFlaskGoogleLogin
constructor. - https: You must use SSL. Simply add
app.run(ssl_context='adhoc', ...)
. - Secret Key: Your app must have a secret key set.
- Accepted Redirect URI: On the Google developer console, your Oauth2
client must list the redirect URI
https://127.0.0.1:5000
as an accepted redirect URI. This is the default redirect URI. You can change this by passing theredirect_uri
argument to theSimpleFlaskGoogleLogin
constructor.
Here's a minimal example with all of these elements.
from flask import Flask, session
from simple_flask_google_login import SimpleFlaskGoogleLogin
app = Flask("Google Login App")
app.secret_key = "YourSecretKeyHere" # Secret key is needed for OAuth 2.0
SimpleFlaskGoogleLogin(app)
@app.route("/")
def index():
if 'name' in session:
return f"Hello {session['name']}! <a href='/logout'>Logout</a>"
return "<a href='/login'>Login</a>"
if __name__ == "__main__":
app.run(ssl_context='adhoc')
And you're done! This is all you need. Now, load the /login
URL for your
application to start the login flow. Load the /logout
URL to logout. For a
minimal example, see the examples/
directory.
Advanced user? Keep reading. Here are several customizations you can make with this utility out of the box.
Advanced: Customize Google Login
You can change any of the usual Google login configurations:
- Client secrets path: This should be a JSON downloaded from the OAuth2 client in your Google developers console. The file will contain both a client ID and a secret.
- Scopes: This is the list of scopes that your application is requesting access to. By default, the application only requests "basic" information such as name and email address.
- Redirect URI: This is the URI that Google will send a GET request to, with the login code attached to it. This must be an https URL -- no IP addresses or custom URL protocols (i.e., deeplinks). By default, this is your application's login callback page.
manager = SimpleFlaskGoogleLogin(
client_secrets_path='/path/to/client_secrets.json',
scopes=['profile', 'email'],
redirect_uri='https://example.com/login/callback'
)
Advanced: Customize routes
You can rename any of the routes. For example, say you want to use /login
for
a general login page with several options. You could then redefine these
routes to be /google/login
, for example.
manager = SimpleFlaskGoogleLogin()
manager.init_app(app, login_endpoint='/google/login')
# creates a login route at `/google/login`
You can add custom handlers to all of the endpoints, to customize different behaviors. For example, open a new browser with the authorization URL, instead of opening in the current tab.
import webbrowser
def handler(authorization_url):
webbrowser.open(authorization_url)
return f"If redirect fails, click <a href='{authorization_url}'>here</a>."
manager = SimpleFlaskGoogleLogin(authorization_url_handler=handler)
Finally, you can ask the manager to completely skip an endpoint and write one
from scratch, by "naming" the endpoint None
.
manager = SimpleFlaskGoogleLogin()
manager.init_app(app, login_endpoint=None)
Why create this?
My previous favorite Flask-GoogleLogin is now deprecated, and I'm too lazy to relearn oauth2 every time I want to support Google login. This library is at least quick starter code. Granted, there are many other implementations, but I wanted a super barebones one -- not one mixed and matched with SQL and flask-login and a slew of other random libraries.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for simple-flask-google-login-0.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3bf15fcf47c28b00cd77b32fa244c4106b1769fcba53b26425aa2c168520b3e |
|
MD5 | 37060184abf217ecb22404768e3fb048 |
|
BLAKE2b-256 | 6274fdf153e60f787d064b386292f59a904bb0bde071d40a82924d1fd70fcab4 |
Hashes for simple_flask_google_login-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf786d6c4948f81cd0c15ec7adca655fac4b2adc7e199afde2089ee99c7e7938 |
|
MD5 | 0dceffd10d0f1a0169685ad500d4d2c1 |
|
BLAKE2b-256 | 5b4f4a3060d1089970dcd8f8cb7a304d1dc41615c2bdff92d27c85112fe786e1 |