# BYOK を使用した Azure マネージド ID

GitHub Copilot SDK の BYOK (独自のキーを持ち込む) では静的 API キーがサポートされていますが、Azureデプロイでは、有効期間の長いキーではなくマネージド ID (Microsoft Entra ID) が使用されることがよくあります。 GitHub Copilot SDK は、柔軟性を最大限に高めるために、Azure Identity SDK で構成するように設計されています。 Azure Identity SDK API を使用して、オンデマンドで新しいトークンをフェッチできるベアラー トークン プロバイダーコールバックを指定します。

<!-- markdownlint-disable GHD046 GHD005 -->

<!-- Suppressed: GHD046 (outdated release terminology), GHD005 (hardcoded data variable) -->

このガイドでは、Azure Identity SDK API を使用して、GitHub Copilot SDK を使用して Microsoft Foundry モデルで認証する方法について説明します。 ほとんどの言語では`DefaultAzureCredential`を使用します。Rust では、`DeveloperToolsCredential`をローカルで使用し、Azureで`ManagedIdentityCredential`します。

## どのように機能するのか

Microsoft Foundry の OpenAI 互換エンドポイント (`https://<resource-name>.openai.azure.com/openai/v1/`) は、静的 API キーの代わりにMicrosoft Entra IDからベアラー トークンを受け入れます。 このガイドでは、GitHub Copilot SDK ランタイムが必要に応じて新しいトークンを要求できるように、トークン プロバイダーのコールバックを使用します。

Pythonを例として使用すると、フローは次のようになります。

1. お使いの環境に合わせて `DefaultAzureCredential` を構成します。
2. BYOK プロバイダーの構成の `bearer_token_provider` に、`DefaultAzureCredential` を使用して `https://ai.azure.com/.default` スコープのトークンを取得するコールバックを渡します。
3. GitHub Copilot SDK は、そのコールバックを介してオンデマンドで新しいトークンを要求できるようにします。

![図: 説明されたプロセスを示すシーケンス図。](/assets/images/help/copilot/copilot-sdk/setup-azure-managed-identity-diagram-0.png)

## コードサンプル

### Prerequisites

言語のAzure ID パッケージと GitHub Copilot SDK パッケージをインストールします。

<div class="ghd-codetabs">
<div class="ghd-codetab" data-lang="dotnet" data-label=".NET"><div class="ghd-codetab-fallback-label" role="heading" aria-level="3">.NET</div>

<!-- docs-validate: skip -->

```bash
dotnet add package GitHub.Copilot.SDK
dotnet add package Azure.Core
```

</div>

<div class="ghd-codetab" data-lang="go" data-label="Go"><div class="ghd-codetab-fallback-label" role="heading" aria-level="3">Go</div>

<!-- docs-validate: skip -->

```bash
go get github.com/github/copilot-sdk/go
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity
```

</div>

<div class="ghd-codetab" data-lang="java" data-label="Java"><div class="ghd-codetab-fallback-label" role="heading" aria-level="3">Java</div>

<!-- docs-validate: skip -->

```xml
<dependency>
    <groupId>com.github</groupId>
    <artifactId>copilot-sdk-java</artifactId>
    <version>${copilot.sdk.version}</version>
</dependency>

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-identity</artifactId>
    <version>${azure.identity.version}</version>
</dependency>
```

</div>

<div class="ghd-codetab" data-lang="python" data-label="Python"><div class="ghd-codetab-fallback-label" role="heading" aria-level="3">Python</div>

<!-- docs-validate: skip -->

```bash
pip install github-copilot-sdk azure-identity
```

</div>

<div class="ghd-codetab" data-lang="rust" data-label="Rust"><div class="ghd-codetab-fallback-label" role="heading" aria-level="3">Rust</div>

<!-- docs-validate: skip -->

```bash
cargo add github-copilot-sdk azure_identity azure_core
cargo add tokio --features macros,rt-multi-thread
```

</div>

<div class="ghd-codetab" data-lang="typescript" data-label="TypeScript"><div class="ghd-codetab-fallback-label" role="heading" aria-level="3">TypeScript</div>

<!-- docs-validate: skip -->

```bash
npm install @github/copilot-sdk @azure/identity
```

</div>

</div>

### トークン プロバイダーコールバックを使用する

この方法は、GitHub Copilot SDK ランタイムが、指定したコールバックを介してオンデマンドで新しいトークンを要求する場合に使用します。 Azure Identity SDK は、トークンのキャッシュと更新のタイミングを処理します。

言語固有の実装を次に示します。

<div class="ghd-codetabs">
<div class="ghd-codetab" data-lang="dotnet" data-label=".NET"><div class="ghd-codetab-fallback-label" role="heading" aria-level="3">.NET</div>

<!-- docs-validate: skip -->

```csharp
using Azure.Core;
using Azure.Identity;
using GitHub.Copilot;

DefaultAzureCredential credential = new(
    DefaultAzureCredential.DefaultEnvironmentVariableName);
await using CopilotClient client = new();
string foundryUrl = Environment.GetEnvironmentVariable("FOUNDRY_RESOURCE_URL")!;

await using CopilotSession session = await client.CreateSessionAsync(new SessionConfig
{
    Model = "gpt-5.5",
    Provider = new ProviderConfig
    {
        Type = "openai",
        BaseUrl = $"{foundryUrl}/openai/v1/",
        BearerTokenProvider = async _ =>
        {
            AccessToken token = await credential.GetTokenAsync(
                new TokenRequestContext(["https://ai.azure.com/.default"]));
            return token.Token;
        },
        WireApi = "responses",
    },
});

AssistantMessageEvent? response = await session.SendAndWaitAsync(
    new MessageOptions { Prompt = "Hello from Managed Identity!" });
Console.WriteLine(response?.Data.Content);
```

</div>

<div class="ghd-codetab" data-lang="go" data-label="Go"><div class="ghd-codetab-fallback-label" role="heading" aria-level="3">Go</div>

<!-- docs-validate: skip -->

```golang
package main

import (
    "context"
    "fmt"
    "log"
    "os"
    "time"

    "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
    "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
    copilot "github.com/github/copilot-sdk/go"
)
func main() {
    opts := azidentity.DefaultAzureCredentialOptions{RequireAzureTokenCredentials: true}
    credential, err := azidentity.NewDefaultAzureCredential(&opts)
    if err != nil {
        log.Fatal(err)
    }

    getBearerToken := func(args copilot.ProviderTokenArgs) (string, error) {
        token, err := credential.GetToken(context.Background(), policy.TokenRequestOptions{
            Scopes: []string{"https://ai.azure.com/.default"},
        })
        if err != nil {
            return "", err
        }
        return token.Token, nil
    }

    client := copilot.NewClient(nil)
    if err := client.Start(context.Background()); err != nil {
        log.Fatal(err)
    }
    defer client.Stop()

    foundryURL := os.Getenv("FOUNDRY_RESOURCE_URL")

    session, err := client.CreateSession(context.Background(), &copilot.SessionConfig{
        Model: "gpt-5.5",
        Provider: &copilot.ProviderConfig{
            Type:                "openai",
            BaseURL:             fmt.Sprintf("%s/openai/v1/", foundryURL),
            BearerTokenProvider: getBearerToken,
            WireAPI:             "responses",
        },
    })
    if err != nil {
        log.Fatal(err)
    }
    defer session.Disconnect()

    ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
    defer cancel()

    response, err := session.SendAndWait(ctx, copilot.MessageOptions{
        Prompt: "Hello from Managed Identity!",
    })
    if err != nil {
        log.Fatal(err)
    }

    if response != nil {
        if data, ok := response.Data.(*copilot.AssistantMessageData); ok {
            fmt.Println(data.Content)
        }
    }
}
```

</div>

<div class="ghd-codetab" data-lang="java" data-label="Java"><div class="ghd-codetab-fallback-label" role="heading" aria-level="3">Java</div>

<!-- docs-validate: skip -->

```java
import com.azure.core.credential.TokenRequestContext;
import com.azure.identity.AzureIdentityEnvVars;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.github.copilot.CopilotClient;
import com.github.copilot.generated.AssistantMessageEvent;
import com.github.copilot.rpc.BearerTokenProvider;
import com.github.copilot.rpc.MessageOptions;
import com.github.copilot.rpc.ProviderConfig;
import com.github.copilot.rpc.SessionConfig;

public class ManagedIdentityExample {
    public static void main(String[] args) throws Exception {
        var credential = new DefaultAzureCredentialBuilder()
                .requireEnvVars(AzureIdentityEnvVars.AZURE_TOKEN_CREDENTIALS)
                .build();
        BearerTokenProvider tokenProvider = providerArgs ->
            credential
                .getToken(new TokenRequestContext().addScopes("https://ai.azure.com/.default"))
                .map(accessToken -> accessToken.getToken())
                .toFuture();
        String foundryUrl = System.getenv("FOUNDRY_RESOURCE_URL");

        try (var client = new CopilotClient()) {
            client.start().get();

            var session = client.createSession(new SessionConfig()
                    .setModel("gpt-5.5")
                    .setProvider(new ProviderConfig()
                            .setType("openai")
                            .setBaseUrl(foundryUrl + "/openai/v1/")
                            .setBearerTokenProvider(tokenProvider)
                            .setWireApi("responses")))
                .get();

            AssistantMessageEvent response = session
                    .sendAndWait(new MessageOptions().setPrompt("Hello from Managed Identity!"))
                    .get();
            System.out.println(response.getData().content());

            session.disconnect().get();
        }
    }
}
```

</div>

<div class="ghd-codetab" data-lang="python" data-label="Python"><div class="ghd-codetab-fallback-label" role="heading" aria-level="3">Python</div>

<!-- docs-validate: skip -->

```python
import asyncio
import os

from azure.identity.aio import DefaultAzureCredential
from copilot import CopilotClient
from copilot.session import PermissionHandler, ProviderConfig

async def main():
    credential = DefaultAzureCredential(require_envvar=True)
    async def get_bearer_token(_args) -> str:
        token = await credential.get_token("https://ai.azure.com/.default")
        return token.token

    foundry_url = os.environ["FOUNDRY_RESOURCE_URL"]

    client = CopilotClient()
    await client.start()

    session = await client.create_session(
        on_permission_request=PermissionHandler.approve_all,
        model="gpt-5.5",
        provider=ProviderConfig(
            type="openai",
            base_url=f"{foundry_url.rstrip('/')}/openai/v1/",
            bearer_token_provider=get_bearer_token,
            wire_api="responses",
        ),
    )

    response = await session.send_and_wait("Hello from Managed Identity!")
    print(response.data.content)

    await client.stop()
    await credential.close()

asyncio.run(main())
```

</div>

<div class="ghd-codetab" data-lang="rust" data-label="Rust"><div class="ghd-codetab-fallback-label" role="heading" aria-level="3">Rust</div>

<!-- docs-validate: skip -->

```rust
use std::sync::Arc;

use azure_core::credentials::TokenCredential;
use azure_identity::{DeveloperToolsCredential, ManagedIdentityCredential};
use github_copilot_sdk::{BearerTokenError, Client, ClientOptions, MessageOptions, ProviderTokenArgs};
use github_copilot_sdk::types::{ProviderConfig, SessionConfig};

fn credential_for_environment() -> azure_core::Result<Arc<dyn TokenCredential>> {
    match std::env::var("AZURE_TOKEN_CREDENTIALS").as_deref() {
        Ok("ManagedIdentityCredential") => Ok(ManagedIdentityCredential::new(None)?),
        _ => Ok(DeveloperToolsCredential::new(None)?),
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let credential = credential_for_environment()?;
    let foundry_url = std::env::var("FOUNDRY_RESOURCE_URL")?;

    let get_bearer_token = {
        let credential = credential.clone();
        move |_args: ProviderTokenArgs| {
            let credential = credential.clone();
            async move {
                let token = credential
                    .get_token(&["https://ai.azure.com/.default"], None)
                    .await
                    .map_err(|err| BearerTokenError::message(err.to_string()))?;
                Ok(token.token.secret().to_string())
            }
        }
    };

    let mut provider = ProviderConfig::default();
    provider.provider_type = Some("openai".to_string());
    provider.base_url = format!("{}/openai/v1/", foundry_url.trim_end_matches('/'));
    provider.bearer_token_provider = Some(Arc::new(get_bearer_token));
    provider.wire_api = Some("responses".to_string());

    let mut config = SessionConfig::default();
    config.model = Some("gpt-5.5".to_string());
    config.provider = Some(provider);

    let client = Client::start(ClientOptions::default()).await?;
    let session = client.create_session(config).await?;

    session
        .send_and_wait(MessageOptions::new("Hello from Managed Identity!"))
        .await?;

    session.disconnect().await?;
    client.stop().await?;
    Ok(())
}
```

</div>

<div class="ghd-codetab" data-lang="typescript" data-label="TypeScript"><div class="ghd-codetab-fallback-label" role="heading" aria-level="3">TypeScript</div>

<!-- docs-validate: skip -->

```typescript
import { DefaultAzureCredential } from "@azure/identity";
import { CopilotClient } from "@github/copilot-sdk";

const credential = new DefaultAzureCredential({
  requiredEnvVars: ["AZURE_TOKEN_CREDENTIALS"],
});
const getBearerToken = async () => {
  const tokenResponse = await credential.getToken("https://ai.azure.com/.default");
  return tokenResponse.token;
};

const client = new CopilotClient();

const session = await client.createSession({
  model: "gpt-5.5",
  provider: {
    type: "openai",
    baseUrl: `${process.env.FOUNDRY_RESOURCE_URL}/openai/v1/`,
    bearerTokenProvider: getBearerToken,
    wireApi: "responses",
  },
});

const response = await session.sendAndWait({ prompt: "Hello from Managed Identity!" });
console.log(response?.data.content);

await client.stop();
```

</div>

</div>

## 環境構成

| 変数                                                                                                                                  | Description                            | Example                                  |
| ----------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------- | ---------------------------------------- |
| `AZURE_TOKEN_CREDENTIALS`                                                                                                           |                                        |                                          |
| **Azure**で実行する場合は、`ManagedIdentityCredential`に設定します。                                                                                |                                        |                                          |
| **ローカルで**実行する場合は、`dev`または開発者ツールの資格情報名 (`AzureCliCredential` など) に設定します。                                                             | `ManagedIdentityCredential`            |                                          |
| `AZURE_CLIENT_ID`                                                                                                                   |                                        |                                          |
| *省略可。*                                                                                                                              |                                        |                                          |
| **Azure**で実行する場合は、`ManagedIdentityCredential`を使用するときに、ユーザー割り当てマネージド ID のクライアント ID に設定します。 設定されていない場合、Azureはシステム割り当てマネージド ID を使用します。 | `11111111-2222-3333-4444-555555555555` |                                          |
| `FOUNDRY_RESOURCE_URL`                                                                                                              | Microsoft Foundry リソースの URL            | `https://<my-resource>.openai.azure.com` |

API キー環境変数は必要ありません。認証はAzure ID 資格情報によって処理されます。 .NET、Go、Java、Python、TypeScript では、`DefaultAzureCredential`は自動的に次をサポートします。

* **マネージド ID** (システム割り当てまたはユーザー割り当て): Azureホストされているアプリの場合
* **Azure CLI** (`az login`): ローカル開発用
* **環境変数**（`AZURE_CLIENT_ID`、`AZURE_TENANT_ID`、`AZURE_CLIENT_SECRET`）: サービス プリンシパル用
* **ワークロード アイデンティティ**: Kubernetes 向け

.NET、Go、Java、Python、TypeScript では、`ManagedIdentityCredential` は、ユーザー割り当てマネージド ID を選択するために `AZURE_CLIENT_ID` を読み取ります。 Rust は、このガイドでは例外です。

Rust では、ローカル開発には `DeveloperToolsCredential` を使用し、Azure で実行する場合は `ManagedIdentityCredential` を使用します。 その他の言語については、完全な資格情報チェーンの `DefaultAzureCredential` ドキュメントを参照してください。

* [.NET](https://aka.ms/azsdk/net/identity/credential-chains#defaultazurecredential-overview)
* [Go](https://aka.ms/azsdk/go/identity/credential-chains#defaultazurecredential-overview)
* [Java](https://aka.ms/azsdk/java/identity/credential-chains#defaultazurecredential-overview)
* [Python](https://aka.ms/azsdk/python/identity/credential-chains#defaultazurecredential-overview)
* [TypeScript](https://aka.ms/azsdk/js/identity/credential-chains#defaultazurecredential-overview)

## このパターンを使用する場合

| シナリオ                                                                  | レコメンデーション |
| --------------------------------------------------------------------- | --------- |
| マネージド ID を使用した Azure でホストされるアプリ                                       |           |
| ✅ このパターンを使用する                                                         |           |
| 既存のMicrosoft Entra サービス プリンシパルを持つアプリ                                  |           |
| ✅ このパターンを使用する                                                         |           |
| `az login` を使用したローカル開発                                                |           |
| ✅ このパターンを使用する                                                         |           |
| 静的 API キーを使用する Azure 以外の環境                                            |           |
| [AUTOTITLE を](/ja/copilot/how-tos/copilot-sdk/auth/byok)使用する          |           |
| 利用可能なGitHub Copilot サブスクリプション                                         |           |
| [AUTOTITLE を](/ja/copilot/how-tos/copilot-sdk/setup/github-oauth)使用する |           |

## こちらも参照ください

* [BYOK (独自のキーを持ち込む)](/ja/copilot/how-tos/copilot-sdk/auth/byok): 静的 API キーの構成
* [バックエンド サービスのセットアップ](/ja/copilot/how-tos/copilot-sdk/setup/backend-services): サーバー側の展開