python logo

Login to your Flask app with Google


Python hosting: Host, run, and code Python in the cloud!

Implementing Google login in your Flask application can greatly simplify user authentication. Rather than requiring users to create another set of credentials, Google login lets them authenticate with existing Google credentials. This tutorial will guide you step-by-step through this process using OAuth, a protocol that facilitates third-party logins.

Why Google Login?

Leveraging Google for user authentication in your Flask application means capitalizing on a widely recognized and trusted platform. Chances are, many of your users already have Google accounts. By offering Google login, you can enhance the user experience by reducing the friction associated with creating and remembering another set of login credentials.

Introduction to OAuth

OAuth is a robust protocol that allows apps to authenticate on behalf of a user, granting them specific permissions without revealing their password. Imagine handing over a car valet key which can unlock your car but can’t open the trunk or start the car. Similarly, with OAuth, users can grant third-party apps limited access to their resources without giving away complete control.

Numerous platforms, like Google, Facebook, and Twitter, rely on OAuth to permit third-party applications to access their resources, ensuring both security and convenience.

Setting Up Google OAuth

  1. Begin by navigating to Google’s API console.
  2. Click on “credentials” and then select “Create new Client ID”.
    Google oAuth Credentials
  3. Fill in the necessary information and proceed by clicking “Create Client ID”. Upon completion, you will be provided with a ‘Client ID’ and ‘Client secret’. Safeguard these details as you’ll need them shortly.
    create oauth

Integrating Google Login in Flask

Flask has a module, flask_oauth, which simplifies the OAuth integration process. This module is maintained by Armin Ronacher, Flask’s creator. It not only supports Google login but also ensures efficient integration with other platforms.

To implement Google login, follow the steps below:

  1. Install flask_oauth:
    This module can be effortlessly installed via pip.
1
pip install flask_oauth
  1. Integrate flask_oauth in your Flask app:
    Replace ‘PUT CLIENT ID’ and ‘PUT CLIENT SECRET’ with the credentials obtained from Google.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from flask import Flask, redirect, url_for, session
from flask_oauth import OAuth

# Configuration details
GOOGLE_CLIENT_ID = 'YOUR CLIENT ID'
GOOGLE_CLIENT_SECRET = 'YOUR CLIENT SECRET'
REDIRECT_URI = '/oauth2callback' # Redirect URI from Google APIs console

app = Flask(__name__)
app.secret_key = 'YOUR SECRET KEY'
oauth = OAuth()

# Google OAuth setup
google = oauth.remote_app(
'google',
base_url='https://www.google.com/accounts/',
authorize_url='https://accounts.google.com/o/oauth2/auth',
request_token_url=None,
request_token_params={
'scope': 'https://www.googleapis.com/auth/userinfo.email',
'response_type': 'code'
},
access_token_url='https://accounts.google.com/o/oauth2/token',
access_token_method='POST',
access_token_params={'grant_type': 'authorization_code'},
consumer_key=GOOGLE_CLIENT_ID,
consumer_secret=GOOGLE_CLIENT_SECRET
)
# ... Rest of the code
  1. Run your Flask app:
1
python app.py

This will start your application, and users should be able to log in via Google. After a successful login, users will be presented with their account information in a JSON format.
Login screen

To further enhance the security of your application, always ensure that the access token is validated across different routes.

For more Flask examples and advanced functionalities, consider downloading this Flask resource.






Leave a Reply:




Topher Mykolyk Tue, 18 Jul 2017

Very nice. Thanks for this straightforward example. Here is an updated version, using the newer library:

https://github.com/lepture/...