この記事では、Microsoft Graph を使用して Python アプリをビルドし、Microsoft Graph ユーザー API を使用したアプリ専用認証 で作成したアプリケーションを拡張します。 Microsoft Graph を使用して、organization内のユーザーを一覧表示します。
次の関数を graph.py に追加します。
async def get_users(self): query_params = UsersRequestBuilder.UsersRequestBuilderGetQueryParameters( # Only request specific properties select = ['displayName', 'id', 'mail'], # Get at most 25 results top = 25, # Sort by display name orderby= ['displayName'] ) request_config = UsersRequestBuilder.UsersRequestBuilderGetRequestConfiguration( query_parameters=query_params ) users = await self.app_client.users.get(request_configuration=request_config) return usersメイン.pyの空の
list_users関数を次のように置き換えます。async def list_users(graph: Graph): users_page = await graph.get_users() # Output each users's details if users_page and users_page.value: for user in users_page.value: print('User:', user.display_name) print(' ID:', user.id) print(' Email:', user.mail) # If @odata.nextLink is present more_available = users_page.odata_next_link is not None print('\nMore users available?', more_available, '\n')アプリを実行し、オプション 2 を選択してユーザーを一覧表示します。
Please choose one of the following options: 0. Exit 1. Display access token 2. List users 3. Make a Graph call 2 User: Adele Vance ID: 05fb57bf-2653-4396-846d-2f210a91d9cf Email: AdeleV@contoso.com User: Alex Wilber ID: a36fe267-a437-4d24-b39e-7344774d606c Email: AlexW@contoso.com User: Allan Deyoung ID: 54cebbaa-2c56-47ec-b878-c8ff309746b0 Email: AllanD@contoso.com User: Bianca Pisani ID: 9a7dcbd0-72f0-48a9-a9fa-03cd46641d49 Email: None User: Brian Johnson (TAILSPIN) ID: a8989e40-be57-4c2e-bf0b-7cdc471e9cc4 Email: BrianJ@contoso.com ... More users available? True
コードの説明
get_users関数のコードについて考えてみましょう。
- ユーザーのコレクションを取得します
-
$selectを使用して特定のプロパティを要求します -
$topを使用して、返されるユーザーの数を制限します -
$orderByを使用して応答を並べ替える