|
| 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