OAuth1 Access Token Retrieval Function

  • Share this:

Code introduction


This function uses the OAuth1 and OAuth1Session classes from the OAuthlib library to obtain an access token by providing client ID, client secret, authorization response, and token URL.


Technology Stack : OAuthlib

Code Type : OAuth1 access token fetcher

Code Difficulty : Intermediate


                
                    
def fetch_access_token(client_id, client_secret, authorization_response, token_url):
    from oauthlib.oauth1 import OAuth1
    from requests_oauthlib import OAuth1Session

    # Create an OAuth1 client
    client = OAuth1(client_id, client_secret)

    # Create an OAuth1Session with the client and the authorization response
    session = OAuth1Session(client, token=authorization_response)

    # Fetch the access token by making a POST request to the token URL
    token_response = session.post(token_url)

    # Extract the access token from the response
    access_token = token_response.json().get('access_token')

    return access_token                
              
Tags: