Package: simple-flask-google-login
Simple Flask Google LoginWritten byAlvin Wanfor educational purposes. See the
accompanying tutorial onHow 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 StartedInstall the utility with pip.pipinstallsimple-flask-google-loginInitialize the utility's manager with the Flask app.fromsimple_flask_google_loginimportSimpleFlaskGoogleLoginSimpleFlaskGoogleLogin(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 expectsclient_secrets.jsonto 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_pathargument to theSimpleFlaskGoogleLoginconstructor.https:You must use SSL. Simply addapp.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 URIhttps://127.0.0.1:5000as an accepted
redirect URI. This is the default redirect URI. You can change this by passing
theredirect_uriargument to theSimpleFlaskGoogleLoginconstructor.Here's a minimal example with all of these elements.fromflaskimportFlask,sessionfromsimple_flask_google_loginimportSimpleFlaskGoogleLoginapp=Flask("Google Login App")app.secret_key="YourSecretKeyHere"# Secret key is needed for OAuth 2.0SimpleFlaskGoogleLogin(app)@app.route("/")defindex():if'name'insession:returnf"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/loginURL for your
application to start the login flow. Load the/logoutURL to logout. For a
minimal example, see theexamples/directory.Advanced user? Keep reading. Here are several customizations you can make
with this utility out of the box.Advanced: Customize Google LoginYou 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 routesYou can rename any of the routes. For example, say you want to use/loginfor
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.importwebbrowserdefhandler(authorization_url):webbrowser.open(authorization_url)returnf"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 endpointNone.manager=SimpleFlaskGoogleLogin()manager.init_app(app,login_endpoint=None)Why create this?My previous favoriteFlask-GoogleLoginis 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 aremanyotherimplementations, but I wanted asuperbarebones one -- not one mixed and
matched withSQL and flask-loginand a slew of other random libraries.