Skip to content

Commit ca11165

Browse files
authored
Merge pull request #158 from microsoftgraph/task/update-samples
Task/update samples
2 parents 8ea9729 + 9e60a8d commit ca11165

7 files changed

Lines changed: 541 additions & 64 deletions

File tree

‎README.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ client = GraphServiceClient(request_adapter)
102102

103103
async def get_user():
104104
user = await client.users.by_user_id('userPrincipalName').get()
105-
print(user.display_name)
105+
if user:
106+
print(user.display_name)
106107

107108
asyncio.run(get_user())
108109
```
@@ -124,7 +125,8 @@ client = GraphServiceClient(request_adapter)
124125

125126
async def me():
126127
me = await client.me.get()
127-
print(me.display_name)
128+
if me:
129+
print(me.display_name)
128130

129131
asyncio.run(me())
130132
```
@@ -151,6 +153,8 @@ asyncio.run(get_user())
151153

152154
* [Microsoft Graph website](https://aka.ms/graph)
153155

156+
* [Samples](samples)
157+
154158
## Upgrading
155159

156160
For detailed information on breaking changes, bug fixes and new functionality introduced during major upgrades, check out our [Upgrade Guide](UPGRADING.md)

‎samples/applications_samples.md‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Create the API Client
2+
3+
```py
4+
import asyncio
5+
6+
from azure.identity import EnvironmentCredential
7+
from kiota_abstractions.api_error import APIError
8+
from kiota_authentication_azure.azure_identity_authentication_provider import AzureIdentityAuthenticationProvider
9+
from msgraph import GraphRequestAdapter
10+
from msgraph import GraphServiceClient
11+
12+
# Set the event loop policy for Windows
13+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
14+
15+
# Create authentication provider object. Used to authenticate request
16+
credential=EnvironmentCredential()
17+
auth_provider = AzureIdentityAuthenticationProvider(credential)
18+
19+
# Initialize a request adapter. Handles the HTTP concerns.
20+
request_adapter = GraphRequestAdapter(auth_provider)
21+
22+
# Get a service client.
23+
client = GraphServiceClient(request_adapter)
24+
```
25+
26+
## 1. LIST ALL APPLICATIONS IN THE TENANT (GET /applications)
27+
28+
```py
29+
async def get_applications():
30+
try:
31+
apps = await client.applications.get()
32+
if apps and apps.value:
33+
for app in apps.value:
34+
print(app.id)
35+
except APIError as e:
36+
print(e.error.message)
37+
asyncio.run(get_applications())
38+
```

‎samples/authentication_samples.md‎

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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())

‎samples/drives_samples.md‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Create the API Client
2+
3+
```py
4+
import asyncio
5+
6+
from azure.identity import ClientSecretCredential
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+
# (Optional) Set the event loop policy for Windows
12+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
13+
14+
# Create authentication provider object. Used to authenticate request
15+
credential = ClientSecretCredential(
16+
tenant_id='TENANT_ID',
17+
client_id='CLIENT_ID',
18+
client_secret='CLIENT_SECRET'
19+
)
20+
scopes = ['https://graph.microsoft.com/.default']
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+
30+
## 1. LIST ALL DRIVES (GET /drives)
31+
32+
```py
33+
async def get_drives():
34+
try:
35+
drives = await client.drives.get()
36+
if drives and drives.value:
37+
for drive in drives.value:
38+
print(drive.id, drive.drive_type, drive.name, drive.description, drive.web_url)
39+
except APIError as e:
40+
print(e.error.message)
41+
asyncio.run(get_drives())
42+
```
43+
44+
## 2. GET DRIVE BY ID (GET /drives/{id})
45+
46+
```py
47+
async def get_drive():
48+
try:
49+
drive = await client.drives.by_drive_id('DRIVE_ID').get()
50+
if drive:
51+
print(drive.id, drive.drive_type, drive.name, drive.description, drive.web_url)
52+
except APIError as e:
53+
print(e.error.message)
54+
asyncio.run(get_drive())
55+
```
56+
57+
## 3. LIST ALL THE ITEMS IN A DRIVE (GET /drives/{id}/items)
58+
59+
```py
60+
async def get_drive_items():
61+
try:
62+
items = await client.drives.by_drive_id('DRIVE_ID').items.get()
63+
if items and items.value:
64+
for item in items.value:
65+
print(item.id, item.name, item.size, item.folder, item.file)
66+
except APIError as e:
67+
print(e.error.message)
68+
asyncio.run(get_drive_items())
69+
```
70+
71+
## 4. GET AN ITEM IN THE DRIVE (GET /drives/{id}/items/{id})
72+
73+
```py
74+
async def get_drive_item():
75+
try:
76+
item = await client.drives.by_drive_id('DRIVE_ID').items.by_drive_item_id('ITEM_ID').get()
77+
if item:
78+
print(item.id, item.name, item.size, item.folder, item.file)
79+
except APIError as e:
80+
print(e.error.message)
81+
asyncio.run(get_drive_item())
82+
```
83+
84+
## 5. GET THE ROOT FOLDER OF THE DRIVE (GET /drives/{id}/root)
85+
86+
```py
87+
async def get_drive_root():
88+
try:
89+
root = await client.drives.by_drive_id('DRIVE_ID').root.get()
90+
if root:
91+
print(root.id, root.name, root.folder.child_count, root.root, root.size)
92+
except APIError as e:
93+
print(e.error.message)
94+
asyncio.run(get_drive_root())
95+
```
96+
97+
## 6. GET ITEMS IN THE ROOT FOLDER OF THE DRIVE (GET drives/{id}/items/root/children)
98+
99+
```py
100+
async def get_drive():
101+
try:
102+
items = await client.drives.by_drive_id('DRIVE_ID').items.by_drive_item_id('root').children.get()
103+
if items and items.value:
104+
for item in items.value:
105+
print(item.id, item.name, item.size, item.folder, item.file)
106+
except APIError as e:
107+
print(e.error.code)
108+
asyncio.run(get_drive())
109+
```

0 commit comments

Comments
 (0)