Search code examples
pythoncallbackazure-web-app-service

Looking for easy way to setup a callback url for a console program requesting an API


I am building a python program that send POST request to a 3rd party API. I am the only user and not building an App for others. I built the program on my Win11 desktop try to query (read only) data from 3rd party.

The problem is the API required 3-Legged OAuth. Based on my understanding, I need to setup a web server and pay for a domain in order to provide a valid Callback URL. Is it true?

Are there are better approaches for my case? I have Azure subscription. Could I setup a static web app for this purpose?


Solution

  • Firstly, Navigate to "Azure Active Directory" > "App registrations" > "New registration", After creation of the app registration then add your endpoints to it as shown in below.

    enter image description here

    • choose the supported account types, and set a redirect URI (you can use http://localhost:5000/callback for local testing).
    • Once the registration is complete, note down the "Application (client) ID" and "Directory (tenant) ID" from the application overview.

    enter image description here

    Implement OAuth Flow in Flask App

    from flask import Flask, request, redirect, url_for, session, jsonify
    from msal import ConfidentialClientApplication
    import requests
    
    app = Flask(__name__)
    app.secret_key = "your_secret_key"
    
    # Azure AD application details
    client_id = "your_client_id"
    client_secret = "your_client_secret"
    authority = "https://login.microsoftonline.com/your_tenant_id"
    
    # 3rd party API details
    api_url = "https://api.example.com/endpoint"
    
    @app.route('/')
    def home():
        return "Home Page"
    
    @app.route('/login')
    def login():
        auth_url = (
            f"{authority}/oauth2/v2.0/authorize?"
            f"client_id={client_id}"
            "&response_type=code"
            "&redirect_uri=http://localhost:5000/callback"
            "&scope=User.Read"  # Adjust scopes based on your API permissions
        )
        return redirect(auth_url)
    
    @app.route('/callback')
    def callback():
        if "code" in request.args:
            code = request.args["code"]
            cca = ConfidentialClientApplication(
                client_id, client_secret, authority,
                post_logout_redirect_uri="http://localhost:5000"
            )
            token_response = cca.acquire_token_by_authorization_code(
                code, scopes=["User.Read"], redirect_uri="http://localhost:5000/callback"
            )
            session["access_token"] = token_response["access_token"]
            return "Authentication successful! You can now make API requests."
    
        return "Authentication failed."
    
    @app.route('/make_api_request')
    def make_api_request():
        access_token = session.get("access_token")
        
        if access_token:
            # Make a sample API request using the obtained access token
            headers = {
                'Authorization': f'Bearer {access_token}',
                'Content-Type': 'application/json'
            }
    
            try:
                # Replace this with the actual payload and endpoint of the 3rd party API
                api_data = {'key1': 'value1', 'key2': 'value2'}
                response = requests.post(api_url, headers=headers, json=api_data)
    
                if response.status_code == 200:
                    return jsonify(response.json())
                else:
                    return f"Error: {response.status_code}\nResponse: {response.text}"
    
            except Exception as e:
                return f"An error occurred: {str(e)}"
        else:
            return "Access token not found. Please authenticate first."
    
    if __name__ == '__main__':
        app.run(port=5000)
    

    The /make_api_request route uses the access token stored in the session to make a sample POST request to the 3rd party API (api_url)

    • Run the Flask app & Visit http://localhost:5000/login in your browser to initiate the OAuth flow. - After authentication, you'll be redirected to the callback URL, and the access token will be stored in the session.

    enter image description here

    Visit http://localhost:5000/make_api_request to make the API request using the obtained access token.