イントロダクション
他の多くの Microsoft サービスと同様に Fabric API を使用するには、まず Fabric サービス用の Microsoft Entra トークンを取得してから、API 呼び出しの承認ヘッダーでそのトークンを使用する必要があります。
このクイック スタート チュートリアルでは、C# コンソール アプリを作成します。このアプリでは、MSAL.Net ライブラリを使用して Entra ID トークンを取得し、C# HttpClient を使用して List ワークスペース API を呼び出します。
アプリの登録を作成する
Microsoft Entra トークンを取得するには、まずアプリケーションを Microsoft Entra ID に登録する必要があります。
アプリケーションの登録とアプリケーションのさまざまなプロパティとそのシナリオへの適用方法の詳細については、「 Microsoft ID プラットフォームにアプリを登録する」を参照してください。
このクイック スタート チュートリアルでは、リダイレクト URI = を使用してパブリック クライアントを作成します。 http://localhost
クラウド アプリケーション管理者以上として Microsoft Entra 管理センターにサインインします。
[アプリケーション] > [アプリの登録] を参照します。
[新規登録] をクリックします。
アプリケーションの表示名を入力し、パブリック クライアント リダイレクト URI を追加する
http://localhost
登録 を選択します。
アプリケーション (クライアント) ID をコピーし、後で使用するためにメモ帳に貼り付けます。
トークンを取得する
このチュートリアルでは、MSAL.Net を使用して、Workspace.ReadWrite.All、Item.ReadWrite.All のスコープを持つ Fabric サービスの Entra ID トークンを取得します。
MSAL.Net を使用したトークン取得の詳細については、「 トークンの取得 - .NET用 Microsoft 認証ライブラリ」を参照してください。
先ほどコピーしたアプリケーション (クライアント) ID を貼り付け、ClientId 変数に貼り付けます。
Microsoft Entra アクセス トークンを取得するための C# コード サンプル
#region parameters section
string ClientId = "YourApplicationId";
string Authority = "https://login.microsoftonline.com/organizations";
string RedirectURI = "http://localhost";
#endregion
#region Acquire a token for Fabric APIs
// In this sample we acquire a token for Fabric service with the scopes
// Workspace.ReadWrite.All and Item.ReadWrite.All
string[] scopes = new string[] { "https://api.fabric.microsoft.com/Workspace.ReadWrite.All https://api.fabric.microsoft.com/Item.ReadWrite.All" };
PublicClientApplicationBuilder PublicClientAppBuilder =
PublicClientApplicationBuilder.Create(ClientId)
.WithAuthority(Authority)
.WithRedirectUri(RedirectURI);
IPublicClientApplication PublicClientApplication = PublicClientAppBuilder.Build();
AuthenticationResult result = await PublicClientApplication.AcquireTokenInteractive(scopes)
.ExecuteAsync()
.ConfigureAwait(false);
Console.WriteLine(result.AccessToken);
#endregion
コールリストワークスペース API
このセクションでは、次のことを行います。
- 前のセクションで取得したトークンを使用して C# HttpClient を作成します。
- クライアントのベース URL として
https://api.fabric.microsoft.com/v1/
を追加します。 - List ワークスペース API を呼び出し、コンソールに応答を書き込みます。
HTTP クライアントの作成と List Workspaces API の呼び出しの C# コード サンプル
// Create client
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
string baseUrl = "https://api.fabric.microsoft.com/v1/";
client.BaseAddress = new Uri(baseUrl);
// Call list workspaces API
HttpResponseMessage response = await client.GetAsync("workspaces");
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
C# コンソール アプリの完全なコード サンプル
using Microsoft.Identity.Client;
using System.Net.Http.Headers;
#region parameters section
string ClientId = "YourApplicationId";
string Authority = "https://login.microsoftonline.com/organizations";
string RedirectURI = "http://localhost";
#endregion
#region Acquire a token for Fabric APIs
// In this sample we acquire a token for Fabric service with the scopes Workspace.ReadWrite.All and Item.ReadWrite.All
string[] scopes = new string[] { "https://api.fabric.microsoft.com/Workspace.ReadWrite.All https://api.fabric.microsoft.com/Item.ReadWrite.All" };
PublicClientApplicationBuilder PublicClientAppBuilder =
PublicClientApplicationBuilder.Create(ClientId)
.WithAuthority(Authority)
.WithRedirectUri(RedirectURI);
IPublicClientApplication PublicClientApplication = PublicClientAppBuilder.Build();
AuthenticationResult result = await PublicClientApplication.AcquireTokenInteractive(scopes)
.ExecuteAsync()
.ConfigureAwait(false);
Console.WriteLine(result.AccessToken);
#endregion
#region Create an HTTP client and call the Fabric APIs
// Create client
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
string baseUrl = "https://api.fabric.microsoft.com/v1/";
client.BaseAddress = new Uri(baseUrl);
// Call list workspaces API
HttpResponseMessage response = await client.GetAsync("workspaces");
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
#endregion