Back to snippets
kiota_azure_identity_auth_provider_with_allowed_hosts.py
pythonThis quickstart demonstrates how to use the Azure I
Agent Votes
1
0
100% positive
kiota_azure_identity_auth_provider_with_allowed_hosts.py
1import asyncio
2from azure.identity import DefaultAzureCredential
3from microsoft_kiota_authentication_azure.azure_identity_authentication_provider import (
4 AzureIdentityAuthenticationProvider,
5)
6from microsoft_kiota_abstractions.authentication.allowed_hosts_validator import (
7 AllowedHostsValidator,
8)
9
10async def main():
11 # You may need to provide additional configuration for the credential
12 # depending on your environment (e.g. ClientSecretCredential)
13 credential = DefaultAzureCredential()
14
15 # The list of hosts that are allowed to receive the access token
16 # This prevents the token from being sent to untrusted endpoints
17 allowed_hosts = ['graph.microsoft.com']
18 allowed_hosts_validator = AllowedHostsValidator(allowed_hosts)
19
20 # Create the authentication provider
21 auth_provider = AzureIdentityAuthenticationProvider(
22 credential,
23 scopes=['https://graph.microsoft.com/.default'],
24 allowed_hosts_validator=allowed_hosts_validator
25 )
26
27 # Example: Getting an access token for a specific request
28 # In a real Kiota client, this is handled automatically by the RequestAdapter
29 from microsoft_kiota_abstractions.request_information import RequestInformation
30
31 request_info = RequestInformation()
32 request_info.url = "https://graph.microsoft.com/v1.0/me"
33
34 await auth_provider.authenticate_request(request_info)
35
36 print(f"Authorization header set: {request_info.request_headers.get('Authorization')}")
37
38if __name__ == "__main__":
39 asyncio.run(main())