3
3
import asyncio
4
4
5
5
import typer
6
+ from azure .core .credentials import AccessToken
6
7
from azure .identity import DeviceCodeCredential
7
8
from msgraph import GraphServiceClient
8
9
from msgraph .generated .users .item .user_item_request_builder import UserItemRequestBuilder
15
16
console = Console ()
16
17
17
18
18
- def get_graph_client () -> tuple [GraphServiceClient , list [str ]]:
19
+ class RawAccessTokenProvider :
20
+ def __init__ (self , access_token : str , expires_on : int ):
21
+ self ._access_token = access_token
22
+ self ._expires_on = expires_on
23
+
24
+ def get_token (self , * scopes , ** kwargs ) -> AccessToken :
25
+ return AccessToken (self ._access_token , self ._expires_on )
26
+
27
+
28
+ def get_graph_client (
29
+ access_token : str | None = None ,
30
+ expires_on : int | None = None ,
31
+ ) -> GraphServiceClient :
19
32
"""Microsoft Graph クライアントを取得する"""
20
33
settings = get_microsoft_graph_settings ()
21
34
scopes = settings .microsoft_graph_user_scopes .split (" " )
22
35
36
+ if access_token and expires_on :
37
+ # アクセストークンが指定されている場合はそれを使用
38
+ access_token_provider = RawAccessTokenProvider (access_token , expires_on )
39
+ return GraphServiceClient (credentials = access_token_provider , scopes = scopes )
23
40
device_code_credential = DeviceCodeCredential (
24
41
client_id = settings .microsoft_graph_client_id ,
25
42
tenant_id = settings .microsoft_graph_tenant_id ,
26
43
)
27
-
28
- client = GraphServiceClient (
44
+ return GraphServiceClient (
29
45
credentials = device_code_credential ,
30
46
scopes = scopes ,
31
47
)
32
48
33
- return client , scopes
34
-
35
49
36
50
@app .command ()
37
51
def get_my_profile (
38
52
fields : list [str ] | None = typer .Option (None , "--field" , "-f" , help = "取得するフィールドを指定(複数指定可能)" ),
53
+ access_token : str | None = typer .Option (None , "--access-token" , "-a" , help = "アクセストークンを指定して認証する" ),
54
+ expires_on : int | None = typer .Option (
55
+ None , "--expires-on" , "-e" , help = "アクセストークンの有効期限を指定(Unix時間)"
56
+ ),
39
57
):
40
58
"""自分のプロファイル情報を取得する"""
41
59
console .print ("[bold green]ユーザープロファイル[/bold green]を取得します" )
@@ -46,7 +64,7 @@ def get_my_profile(
46
64
47
65
async def _get_profile ():
48
66
try :
49
- client , _ = get_graph_client ()
67
+ client = get_graph_client (access_token = access_token , expires_on = expires_on )
50
68
51
69
query_params = UserItemRequestBuilder .UserItemRequestBuilderGetQueryParameters (select = select_fields )
52
70
request_config = UserItemRequestBuilder .UserItemRequestBuilderGetRequestConfiguration (
@@ -90,9 +108,8 @@ def get_access_token():
90
108
91
109
access_token = device_code_credential .get_token (* scopes )
92
110
93
- console .print ("[bold green]トークン取得成功[/bold green]" )
94
- console .print (f" トークン: { access_token .token } " )
95
- console .print (f" 期限: { access_token .expires_on } " )
111
+ print (f"access_token: { access_token .token } " )
112
+ print (f"expires_on: { access_token .expires_on } " )
96
113
97
114
except Exception as e :
98
115
console .print (f"[bold red]エラー[/bold red]: { str (e )} " )
0 commit comments