Implementing social login features in your Python web application can significantly enhance user experience by allowing users to sign in using their existing Google or Facebook accounts. This step-by-step tutorial guides you through integrating Google and Facebook login functionalities into your Python project.

Prerequisites

  • Python installed on your system (Python 3.6+ recommended)
  • Basic knowledge of Python and web development
  • Flask web framework installed
  • Register applications on Google Developer Console and Facebook for Developers
  • Install necessary Python packages: Flask, requests, oauthlib, and Flask-Dance

Registering Your Applications

Google: Visit the Google Developer Console, create a new project, enable the Google+ API, and create OAuth 2.0 credentials. Set the authorized redirect URI to http://localhost:5000/login/google/authorized.

Facebook: Go to Facebook for Developers, create a new app, and set up Facebook Login. Add the redirect URI http://localhost:5000/login/facebook/authorized in the app settings.

Installing Required Packages

Run the following command to install necessary Python packages:

pip install Flask requests Flask-Dance

Setting Up the Flask Application

Create a new Python file, app.py, and add the following code to set up the Flask app with Google and Facebook OAuth integrations.

from flask import Flask, redirect, url_for
from flask_dance.contrib.google import make_google_blueprint, google
from flask_dance.contrib.facebook import make_facebook_blueprint, facebook

app = Flask(__name__)
app.secret_key = 'your_secret_key_here'

# Configure Google OAuth
google_bp = make_google_blueprint(
    client_id='YOUR_GOOGLE_CLIENT_ID',
    client_secret='YOUR_GOOGLE_CLIENT_SECRET',
    scope=['profile', 'email'],
    redirect_url='/login/google/authorized'
)
app.register_blueprint(google_bp, url_prefix='/login')

# Configure Facebook OAuth
facebook_bp = make_facebook_blueprint(
    client_id='YOUR_FACEBOOK_APP_ID',
    client_secret='YOUR_FACEBOOK_APP_SECRET',
    scope=['public_profile', 'email'],
    redirect_url='/login/facebook/authorized'
)
app.register_blueprint(facebook_bp, url_prefix='/login')

@app.route('/')
def index():
    return '''
        

Social Login Demo

Login with Google
Login with Facebook ''' @app.route('/login/google') def login_google(): if not google.authorized: return redirect(url_for('google.login')) resp = google.get('/oauth2/v2/userinfo') assert resp.ok, resp.text user_info = resp.json() return f'Hello, {user_info["name"]}! Your email is {user_info["email"]}.' @app.route('/login/facebook') def login_facebook(): if not facebook.authorized: return redirect(url_for('facebook.login')) resp = facebook.get('/me?fields=name,email') assert resp.ok, resp.text user_info = resp.json() return f'Hello, {user_info["name"]}! Your email is {user_info.get("email", "Not available")}.' if __name__ == '__main__': app.run(debug=True)

Running Your Application

Replace YOUR_GOOGLE_CLIENT_ID, YOUR_GOOGLE_CLIENT_SECRET, YOUR_FACEBOOK_APP_ID, and YOUR_FACEBOOK_APP_SECRET with the credentials obtained from the developer consoles. Save the app.py file and run your Flask app:

python app.py

Open your browser and navigate to http://localhost:5000/. Click on the login links to authenticate via Google or Facebook.

Conclusion

Integrating social login with Google and Facebook in your Python web application enhances user convenience and can increase registration rates. This tutorial provides a foundational setup; you can further customize the user experience and handle user data as needed.