OAuth2 Provider Identifier Fetcher

  • Share this:

Code introduction


This function is used to retrieve the user identifier from a specified OAuth2 provider. It uses the OAuth2Client and OAuth2Token classes from the Authlib library to send an HTTP request to obtain an access token and ultimately retrieve the provider's user identifier.


Technology Stack : Authlib, OAuth2Client, OAuth2Token

Code Type : Function

Code Difficulty : Intermediate


                
                    
def random_provider_identifier(provider_name):
    from authlib.integrations.oauth2.client import OAuth2Client
    from authlib.oauth2 import OAuth2Token

    client = OAuth2Client(client_id='client_id', client_secret='client_secret')
    token = OAuth2Token(client=client)
    token.set_authorization_header({'Authorization': f'Bearer {token.token}'})
    response = token.fetch_token(token_url='https://example.com/oauth2/token', authorization_url='https://example.com/oauth2/authorize', resource_owner_username=provider_name)
    return response.get('provider_identifier')