|
| 1 | + |
| 2 | +# DELEGATED ACCESS SAMPLES (REQUIRES SIGNED IN USER) |
| 3 | + |
| 4 | +## 1. DEVICE CODE FLOW |
| 5 | +```py |
| 6 | +import asyncio |
| 7 | + |
| 8 | +from azure.identity import DeviceCodeCredential |
| 9 | +from kiota_abstractions.api_error import APIError |
| 10 | +from kiota_authentication_azure.azure_identity_authentication_provider import AzureIdentityAuthenticationProvider |
| 11 | +from msgraph import GraphRequestAdapter, GraphServiceClient |
| 12 | + |
| 13 | +# Set the event loop policy for Windows |
| 14 | +asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) |
| 15 | + |
| 16 | +# Create authentication provider object. Used to authenticate requests |
| 17 | +credential = DeviceCodeCredential( |
| 18 | + client_id='CLIENT_ID', |
| 19 | + tenant_id='TENANT_ID', |
| 20 | + ) |
| 21 | + |
| 22 | +scopes = ["User.Read"] |
| 23 | +auth_provider = AzureIdentityAuthenticationProvider(credential, scopes=scopes) |
| 24 | + |
| 25 | +# Initialize a request adapter with the auth provider. |
| 26 | +request_adapter = GraphRequestAdapter(auth_provider) |
| 27 | + |
| 28 | +# Create an API client with the request adapter. |
| 29 | +client = GraphServiceClient(request_adapter) |
| 30 | + |
| 31 | +# GET A USER USING THE USER ID (GET /users/{id}) |
| 32 | +async def get_user(): |
| 33 | + try: |
| 34 | + user = await client.users_by_id('USER_ID').get() |
| 35 | + if user: |
| 36 | + print(user.user_principal_name, user.display_name, user.id) |
| 37 | + except APIError as e: |
| 38 | + print(f'Error: {e.error.message}') |
| 39 | + |
| 40 | +asyncio.run(get_user()) |
| 41 | +``` |
| 42 | + |
| 43 | +## 2. INTERACTIVE BROSWER FLOW |
| 44 | + |
| 45 | +```py |
| 46 | +import asyncio |
| 47 | +from azure.identity import InteractiveBrowserCredential |
| 48 | +from kiota_abstractions.api_error import APIError |
| 49 | +from kiota_authentication_azure.azure_identity_authentication_provider import AzureIdentityAuthenticationProvider |
| 50 | +from msgraph import GraphRequestAdapter, GraphServiceClient |
| 51 | + |
| 52 | +# Set the event loop policy for Windows |
| 53 | +asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) |
| 54 | + |
| 55 | +# Create authentication provider object. Used to authenticate requests |
| 56 | +credential = InteractiveBrowserCredential() |
| 57 | +scopes = ["User.Read"] |
| 58 | +auth_provider = AzureIdentityAuthenticationProvider(credential, scopes=scopes) |
| 59 | + |
| 60 | +# Initialize a request adapter with the auth provider. |
| 61 | +request_adapter = GraphRequestAdapter(auth_provider) |
| 62 | + |
| 63 | +# Create an API client with the request adapter. |
| 64 | +client = GraphServiceClient(request_adapter) |
| 65 | + |
| 66 | +# GET A USER USING THE USER ID (GET /users/{id}) |
| 67 | +async def get_user(): |
| 68 | + try: |
| 69 | + user = await client.users_by_id('USER_ID').get() |
| 70 | + if user: |
| 71 | + print(user.user_principal_name, user.display_name, user.id) |
| 72 | + except APIError as e: |
| 73 | + print(f'Error: {e.error.message}') |
| 74 | +asyncio.run(get_user()) |
| 75 | +``` |
| 76 | + |
| 77 | +# APPLICATION ACCESS SAMPLES (APPLICATIONS ONLY) |
| 78 | + |
| 79 | +## 3. CLIENT SECRET CREDENTIALS FLOW |
| 80 | + |
| 81 | +```py |
| 82 | +import asyncio |
| 83 | + |
| 84 | +from azure.identity import ClientSecretCredential |
| 85 | +from kiota_abstractions.api_error import APIError |
| 86 | +from kiota_authentication_azure.azure_identity_authentication_provider import AzureIdentityAuthenticationProvider |
| 87 | +from msgraph import GraphRequestAdapter, GraphServiceClient |
| 88 | + |
| 89 | +# Set the event loop policy for Windows |
| 90 | +asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) |
| 91 | + |
| 92 | +# Create authentication provider object. Used to authenticate request |
| 93 | +credential = ClientSecretCredential( |
| 94 | + tenant_id='TENANT_ID', |
| 95 | + client_id='CLIENT_ID', |
| 96 | + client_secret='CLIENT_SECRET' |
| 97 | +) |
| 98 | +scopes = ['https://graph.microsoft.com/.default'] |
| 99 | +auth_provider = AzureIdentityAuthenticationProvider(credential, scopes=scopes) |
| 100 | + |
| 101 | +# Initialize a request adapter with the auth provider. |
| 102 | +request_adapter = GraphRequestAdapter(auth_provider) |
| 103 | + |
| 104 | +# Create an API client with the request adapter. |
| 105 | +client = GraphServiceClient(request_adapter) |
| 106 | + |
| 107 | +# GET A USER USING THE USER ID (GET /users/{id}) |
| 108 | +async def get_user(): |
| 109 | + try: |
| 110 | + user = await client.users.by_user_id('USER_ID').get() |
| 111 | + if user: |
| 112 | + print(user.user_principal_name, user.display_name, user.id) |
| 113 | + except APIError as e: |
| 114 | + print(f'Error: {e.error.message}') |
| 115 | +asyncio.run(get_user()) |
| 116 | +``` |
| 117 | + |
| 118 | +## 4. ENVIRONMENT CREDENTIAL FLOW (ASYNC) |
| 119 | + |
| 120 | +```py |
| 121 | +import asyncio |
| 122 | + |
| 123 | +from azure.identity.aio import EnvironmentCredential |
| 124 | +from kiota_abstractions.api_error import APIError |
| 125 | +from kiota_authentication_azure.azure_identity_authentication_provider import AzureIdentityAuthenticationProvider |
| 126 | +from msgraph import GraphRequestAdapter, GraphServiceClient |
| 127 | + |
| 128 | +# Set the event loop policy for Windows |
| 129 | +asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) |
| 130 | + |
| 131 | +# Create authentication provider object. Used to authenticate request |
| 132 | +credential = EnvironmentCredential() |
| 133 | +scopes = ['https://graph.microsoft.com/.default'] |
| 134 | +auth_provider = AzureIdentityAuthenticationProvider(credential, scopes=scopes) |
| 135 | + |
| 136 | +# Initialize a request adapter with the auth provider. |
| 137 | +request_adapter = GraphRequestAdapter(auth_provider) |
| 138 | + |
| 139 | +# Create an API client with the request adapter. |
| 140 | +client = GraphServiceClient(request_adapter) |
| 141 | + |
| 142 | +# GET A USER USING THE USER ID (GET /users/{id}) |
| 143 | +async def get_user(): |
| 144 | + try: |
| 145 | + user = await client.users.by_user_id('USER_ID').get() |
| 146 | + if user: |
| 147 | + print(user.user_principal_name, user.display_name, user.id) |
| 148 | + except APIError as e: |
| 149 | + print(f'Error: {e.error.message}') |
| 150 | + |
| 151 | +asyncio.run(get_user()) |
0 commit comments