OAuth1 Access Token Fetcher

  • Share this:

Code introduction


This function uses the OAuth1 class from the OAuthlib library and the requests library to obtain an access token from the authorization server. It accepts client ID, client secret, token URL, and authorization response as parameters.


Technology Stack : Python, OAuthlib, requests

Code Type : Function

Code Difficulty : Intermediate


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

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

    # Send a POST request to the token URL with the authorization response
    response = post(token_url, data={'oauth_token': authorization_response['oauth_token'],
                                    'oauth_verifier': authorization_response['oauth_verifier']},
                                    auth=client)

    # Parse the response to get the access token
    token = response.json()

    return token