소개
다른 많은 Microsoft 서비스와 마찬가지로 Fabric API를 사용하려면 먼저 Fabric 서비스에 대한 Microsoft Entra 토큰을 가져오고 API 호출의 권한 부여 헤더에서 해당 토큰을 사용해야 합니다.
이 빠른 시작 자습서에서는 MSAL.Net 라이브러리를 사용하여 Entra ID 토큰을 획득한 다음 C# HttpClient를 사용하여 목록 작업 영역 API를 호출하는 C# 콘솔 앱을 만듭니다.
앱 등록 만들기
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/합니다. - 목록 작업 영역 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