Creating OAuth Client for Flask with Authlib

  • Share this:

Code introduction


This function uses Authlib's Flask client integration to create an OAuth client for authentication with third-party services and accessing resources. It initializes an OAuth instance, registers a client named 'hypothetical_service', and sets the relevant authentication and API base URLs.


Technology Stack : Authlib, Flask

Code Type : Flask extension

Code Difficulty : Intermediate


                
                    
from authlib.integrations.flask_client import OAuth
from flask import Flask

def create_oauth_client(app):
    # Initialize OAuth with Flask app
    oauth = OAuth(app)

    # Define the OAuth client for a hypothetical service
    oauth.register(
        name='hypothetical_service',
        client_id='YOUR_CLIENT_ID',
        client_secret='YOUR_CLIENT_SECRET',
        access_token_url='https://hypothetical-service.com/oauth/token',
        access_token_params=None,
        authorize_url='https://hypothetical-service.com/oauth/authorize',
        authorize_params=None,
        api_base_url='https://hypothetical-service.com/api',
        client_kwargs={'scope': 'profile email'}
    )

    return oauth                
              
Tags: