Skip to content

Commit 418d40f

Browse files
committed
add raw token injection
1 parent aa9bb28 commit 418d40f

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

docs/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ uv run python scripts/microsoft_graphs.py --help
125125
uv run python scripts/microsoft_graphs.py get-access-token
126126

127127
# Get my profile
128-
uv run python scripts/microsoft_graphs.py get-my-profile
128+
uv run python scripts/microsoft_graphs.py get-my-profile \
129+
--access-token $ACCESS_TOKEN \
130+
--expires-on $EXPIRES_ON
129131
```
130132

131133
## MCP

scripts/microsoft_graphs.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import asyncio
44

55
import typer
6+
from azure.core.credentials import AccessToken
67
from azure.identity import DeviceCodeCredential
78
from msgraph import GraphServiceClient
89
from msgraph.generated.users.item.user_item_request_builder import UserItemRequestBuilder
@@ -15,27 +16,44 @@
1516
console = Console()
1617

1718

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:
1932
"""Microsoft Graph クライアントを取得する"""
2033
settings = get_microsoft_graph_settings()
2134
scopes = settings.microsoft_graph_user_scopes.split(" ")
2235

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)
2340
device_code_credential = DeviceCodeCredential(
2441
client_id=settings.microsoft_graph_client_id,
2542
tenant_id=settings.microsoft_graph_tenant_id,
2643
)
27-
28-
client = GraphServiceClient(
44+
return GraphServiceClient(
2945
credentials=device_code_credential,
3046
scopes=scopes,
3147
)
3248

33-
return client, scopes
34-
3549

3650
@app.command()
3751
def get_my_profile(
3852
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+
),
3957
):
4058
"""自分のプロファイル情報を取得する"""
4159
console.print("[bold green]ユーザープロファイル[/bold green]を取得します")
@@ -46,7 +64,7 @@ def get_my_profile(
4664

4765
async def _get_profile():
4866
try:
49-
client, _ = get_graph_client()
67+
client = get_graph_client(access_token=access_token, expires_on=expires_on)
5068

5169
query_params = UserItemRequestBuilder.UserItemRequestBuilderGetQueryParameters(select=select_fields)
5270
request_config = UserItemRequestBuilder.UserItemRequestBuilderGetRequestConfiguration(
@@ -90,9 +108,8 @@ def get_access_token():
90108

91109
access_token = device_code_credential.get_token(*scopes)
92110

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}")
96113

97114
except Exception as e:
98115
console.print(f"[bold red]エラー[/bold red]: {str(e)}")

0 commit comments

Comments
 (0)