OAuth2 User Details Retrieval with Authlib

  • Share this:

Code introduction


This function uses the Authlib library to fetch user details from an OAuth2 provider. It takes a user ID and an OAuth2 session object as inputs, then uses the session to obtain a token and fetch user details.


Technology Stack : Authlib, OAuth2

Code Type : Function

Code Difficulty : Intermediate


                
                    
def get_user_details(user_id, session):
    """
    Fetches user details from an OAuth2 provider using Authlib.

    Args:
        user_id (str): The user ID to fetch details for.
        session (authlib.integrations.oauth2.Session): An OAuth2 session object.

    Returns:
        dict: A dictionary containing user details.
    """
    token = session.get_token()
    details = session.provider.users.get(user_id, token=token)
    return details

# JSON representation of the code                
              
Tags: