{"library":"microsoft-kiota-authentication-azure","code":"import os\nimport asyncio\nfrom azure.identity import DeviceCodeCredential\nfrom microsoft_kiota_authentication_azure import AzureIdentityAuthenticationProvider\nfrom microsoft_kiota_http.httpx_request_adapter import HttpxRequestAdapter\nfrom kiota_abstractions.request_information import RequestInformation, HttpMethod\nfrom kiota_abstractions.serialization import ParseNodeFactoryRegistry, SerializationWriterFactoryRegistry\n\nasync def main():\n    # 1. Obtain a TokenCredential from azure-identity.\n    # For local development, DeviceCodeCredential is often used.\n    # Ensure you have registered an application in Azure AD and have its CLIENT_ID.\n    # Set AZURE_CLIENT_ID environment variable or pass client_id directly.\n    client_id = os.environ.get('AZURE_CLIENT_ID', 'YOUR_CLIENT_ID_HERE')\n    if client_id == 'YOUR_CLIENT_ID_HERE':\n        print(\"Please set the AZURE_CLIENT_ID environment variable or replace 'YOUR_CLIENT_ID_HERE'.\")\n        return\n\n    credential = DeviceCodeCredential(client_id=client_id)\n\n    # 2. Create the AzureIdentityAuthenticationProvider.\n    # The allowed_hosts list is crucial for security, specifying which domains\n    # the authentication provider is allowed to send tokens to.\n    allowed_hosts = [\"graph.microsoft.com\", \"yourtenant.onmicrosoft.com\"]\n    auth_provider = AzureIdentityAuthenticationProvider(credential, allowed_hosts)\n\n    # 3. Create a RequestAdapter using the authentication provider.\n    # HttpxRequestAdapter is Kiota's default HTTP client implementation.\n    # You also need a ParseNodeFactoryRegistry and SerializationWriterFactoryRegistry\n    # for a fully functional adapter, even if not directly used in this simple auth example.\n    request_adapter = HttpxRequestAdapter(\n        auth_provider,\n        parse_node_factory=ParseNodeFactoryRegistry(),\n        serialization_writer_factory=SerializationWriterFactoryRegistry()\n    )\n\n    # 4. Illustrative use: Create a RequestInformation object and send it (conceptually).\n    # In a real scenario, this would be part of a Kiota-generated API client call.\n    request_info = RequestInformation(HttpMethod.GET, \"https://graph.microsoft.com/v1.0/me\")\n    print(f\"\\nAttempting to authenticate request to: {request_info.url_template}\")\n    \n    # The authenticate_request method is typically called internally by the RequestAdapter.\n    # We call it here to demonstrate its direct usage and token acquisition.\n    try:\n        await auth_provider.authenticate_request(request_info)\n        print(\"Authentication provider prepared the request with a token.\")\n        if request_info.headers and 'Authorization' in request_info.headers:\n            print(\"Authorization header added successfully.\")\n        else:\n            print(\"Authorization header not found after authentication.\")\n        # In a real app, you'd then use request_adapter.send(request_info, ...) to make the call\n        print(\"\\nQuickstart setup complete. You would now use this request_adapter with a Kiota-generated client.\")\n    except Exception as e:\n        print(f\"An error occurred during authentication: {e}\")\n        print(\"Ensure your AZURE_CLIENT_ID is correct and your application registration supports the chosen credential type (e.g., Device Code Flow).\")\n\nif __name__ == '__main__':\n    asyncio.run(main())","lang":"python","description":"This quickstart demonstrates how to set up the `AzureIdentityAuthenticationProvider` with an `azure-identity` credential (like `DeviceCodeCredential`) and integrate it into a `HttpxRequestAdapter`. It highlights the essential steps to prepare an authenticated request, typically used by a Kiota-generated API client. Remember to replace 'YOUR_CLIENT_ID_HERE' or set the `AZURE_CLIENT_ID` environment variable with your Azure AD application's client ID. Also, configure your Azure application registration to support the Device Code Flow for this example.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}