Skip to content

Commit 8f0980c

Browse files
committed
Add auth samples
1 parent 5ddd267 commit 8f0980c

1 file changed

Lines changed: 152 additions & 0 deletions

File tree

‎docs/authentication_samples.md‎

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
2+
## 1. DEVICE CODE FLOW
3+
```py
4+
import asyncio
5+
6+
from azure.identity import DeviceCodeCredential
7+
from kiota_abstractions.api_error import APIError
8+
from kiota_authentication_azure.azure_identity_authentication_provider import AzureIdentityAuthenticationProvider
9+
from msgraph import GraphRequestAdapter, GraphServiceClient
10+
11+
# Set the event loop policy for Windows
12+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
13+
14+
# Create authentication provider object. Used to authenticate requests
15+
credential = DeviceCodeCredential(
16+
client_id='CLIENT_ID',
17+
tenant_id='TENANT_ID',
18+
)
19+
20+
scopes = ["User.Read"]
21+
auth_provider = AzureIdentityAuthenticationProvider(credential, scopes=scopes)
22+
23+
# Initialize a request adapter with the auth provider.
24+
request_adapter = GraphRequestAdapter(auth_provider)
25+
26+
# Create an API client with the request adapter.
27+
client = GraphServiceClient(request_adapter)
28+
29+
# GET A USER USING THE USER ID
30+
# Request: GET /users/{id}
31+
async def get_user():
32+
try:
33+
user = await client.users_by_id('USER_ID').get()
34+
if user:
35+
print(user.user_principal_name, user.display_name, user.id)
36+
except APIError as e:
37+
print(f'Error: {e.error.message}')
38+
39+
asyncio.run(get_user())
40+
```
41+
42+
2. ## INTERACTIVE BROSWER FLOW
43+
44+
```py
45+
import asyncio
46+
from azure.identity import InteractiveBrowserCredential
47+
from kiota_abstractions.api_error import APIError
48+
from kiota_authentication_azure.azure_identity_authentication_provider import AzureIdentityAuthenticationProvider
49+
from msgraph import GraphRequestAdapter, GraphServiceClient
50+
51+
# Set the event loop policy for Windows
52+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
53+
54+
# Create authentication provider object. Used to authenticate requests
55+
credential = InteractiveBrowserCredential()
56+
scopes = ["User.Read"]
57+
auth_provider = AzureIdentityAuthenticationProvider(credential, scopes=scopes)
58+
59+
# Initialize a request adapter with the auth provider.
60+
request_adapter = GraphRequestAdapter(auth_provider)
61+
62+
# Create an API client with the request adapter.
63+
client = GraphServiceClient(request_adapter)
64+
65+
# GET A USER USING THE USER ID
66+
# Request: GET /users/{id}
67+
68+
async def get_user():
69+
try:
70+
user = await client.users_by_id('USER_ID').get()
71+
if user:
72+
print(user.user_principal_name, user.display_name, user.id)
73+
except APIError as e:
74+
print(f'Error: {e.error.message}')
75+
asyncio.run(get_user())
76+
```
77+
78+
## 3. CLIENT SECRET CREDENTIALS FLOW
79+
80+
```py
81+
import asyncio
82+
83+
from azure.identity import ClientSecretCredential
84+
from kiota_abstractions.api_error import APIError
85+
from kiota_authentication_azure.azure_identity_authentication_provider import AzureIdentityAuthenticationProvider
86+
from msgraph import GraphRequestAdapter, GraphServiceClient
87+
88+
# Set the event loop policy for Windows
89+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
90+
91+
# Create authentication provider object. Used to authenticate request
92+
credential = ClientSecretCredential(
93+
tenant_id='TENANT_ID',
94+
client_id='CLIENT_ID',
95+
client_secret='CLIENT_SECRET'
96+
)
97+
scopes = ['https://graph.microsoft.com/.default']
98+
auth_provider = AzureIdentityAuthenticationProvider(credential, scopes=scopes)
99+
100+
# Initialize a request adapter with the auth provider.
101+
request_adapter = GraphRequestAdapter(auth_provider)
102+
103+
# Create an API client with the request adapter.
104+
client = GraphServiceClient(request_adapter)
105+
106+
# GET A USER USING THE USER ID
107+
# Request: GET /users/{id}
108+
async def get_user():
109+
try:
110+
user = await client.users_by_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
143+
# Request: GET /users/{id}
144+
async def get_user():
145+
try:
146+
user = await client.users_by_id('USER_ID').get()
147+
if user:
148+
print(user.user_principal_name, user.display_name, user.id)
149+
except APIError as e:
150+
print(f'Error: {e.error.message}')
151+
152+
asyncio.run(get_user())

0 commit comments

Comments
 (0)