File tree Expand file tree Collapse file tree 3 files changed +74
-0
lines changed
template_fastapi/settings Expand file tree Collapse file tree 3 files changed +74
-0
lines changed Original file line number Diff line number Diff line change @@ -21,3 +21,9 @@ AZURE_AI_SPEECH_ENDPOINT="https://<speech-api-name>.cognitiveservices.azure.com/
21
21
# Chats WebSocket
22
22
# Azure Container Apps: `wss://yourcontainerapps.japaneast.azurecontainerapps.io`
23
23
CHATS_WEBSOCKET_URL = " ws://localhost:8000"
24
+
25
+ # Microsoft Graph Sites
26
+ MICROSOFT_GRAPH_TENANT_ID = " <YOUR_TENANT_ID>"
27
+ MICROSOFT_GRAPH_CLIENT_ID = " <YOUR_CLIENT_ID>"
28
+ MICROSOFT_GRAPH_CLIENT_SECRET = " <YOUR_CLIENT_SECRET>"
29
+ MICROSOFT_GRAPH_USER_SCOPES = " User.Read Sites.Read.All" # scopes for Microsoft Graph API
Original file line number Diff line number Diff line change
1
+ import asyncio
2
+
3
+ from azure .identity import DeviceCodeCredential
4
+ from msgraph import GraphServiceClient
5
+ from msgraph .generated .users .item .user_item_request_builder import UserItemRequestBuilder
6
+
7
+ from template_fastapi .settings .microsoft_graphs import get_microsoft_graph_settings
8
+
9
+
10
+ async def main ():
11
+ # Load settings
12
+ settings = get_microsoft_graph_settings ()
13
+ scopes = settings .microsoft_graph_user_scopes .split (" " )
14
+
15
+ print (settings .model_dump_json (indent = 2 ))
16
+ print (scopes )
17
+
18
+ # Add authentication
19
+ device_code_credential = DeviceCodeCredential (
20
+ client_id = settings .microsoft_graph_client_id ,
21
+ tenant_id = settings .microsoft_graph_tenant_id ,
22
+ )
23
+ # access_token = device_code_credential.get_token(*scopes)
24
+ # print(f"Access Token: {access_token.token}")
25
+
26
+ # Get user profile
27
+ # Only request specific properties using $select
28
+ query_params = UserItemRequestBuilder .UserItemRequestBuilderGetQueryParameters (
29
+ select = [
30
+ "displayName" ,
31
+ "mail" ,
32
+ "userPrincipalName" ,
33
+ ]
34
+ )
35
+ request_config = UserItemRequestBuilder .UserItemRequestBuilderGetRequestConfiguration (query_parameters = query_params )
36
+ user_client = GraphServiceClient (
37
+ credentials = device_code_credential ,
38
+ scopes = scopes ,
39
+ )
40
+
41
+ user = await user_client .me .get (request_configuration = request_config )
42
+ print (f"User: { user .display_name } ({ user .mail } )" )
43
+
44
+
45
+ # Run main
46
+ asyncio .run (main ())
Original file line number Diff line number Diff line change
1
+ from functools import lru_cache
2
+
3
+ from pydantic_settings import BaseSettings , SettingsConfigDict
4
+
5
+
6
+ class Settings (BaseSettings ):
7
+ chats_websocket_url : str = "ws://localhost:8000"
8
+ microsoft_graph_tenant_id : str = "<YOUR_TENANT_ID>"
9
+ microsoft_graph_client_id : str = "<YOUR_CLIENT_ID>"
10
+ microsoft_graph_client_secret : str = "<YOUR_CLIENT_SECRET>"
11
+ microsoft_graph_user_scopes : str = "User.Read Sites.Read.All" # Space-separated scopes
12
+
13
+ model_config = SettingsConfigDict (
14
+ env_file = ".env" ,
15
+ env_ignore_empty = True ,
16
+ extra = "ignore" ,
17
+ )
18
+
19
+
20
+ @lru_cache
21
+ def get_microsoft_graph_settings () -> Settings :
22
+ return Settings ()
You can’t perform that action at this time.
0 commit comments