Fabric API 快速入门

介绍

若要与许多其他Microsoft服务一样使用 Fabric API,首先需要获取 Fabric 服务的 Microsoft Entra 令牌,然后在 API 调用的授权标头中使用该令牌。

在本快速入门教程中,你将创建一个 C# 控制台应用,该应用将使用 MSAL.Net 库获取 Entra ID 令牌,然后使用 C# HttpClient 调用列表工作区 API。

创建应用注册

若要获取Microsoft Entra 令牌,首先需要使用 Microsoft Entra ID 注册应用程序。

若要详细了解如何注册应用程序和应用程序的不同属性及其可能适用于你的方案,请参阅 在Microsoft标识平台中注册应用

在这篇快速入门教程中,你将创建一个公共客户端,并指定重定向 URI = http://localhost

  1. 至少以云应用程序管理员身份登录到 Microsoft Entra 管理中心

  2. 浏览到应用 > 应用程序注册。

  3. 单击“新建注册”。

  4. 输入应用程序的显示名称,并添加公共客户端重定向 URI http://localhost

    显示应用注册形式的屏幕截图。

  5. 选择“注册”。

  6. 复制 应用程序(客户端)ID 并将其粘贴到记事本中,以供稍后使用。

显示在 Azure 中注册后应用的屏幕截图。

获取令牌

在本教程中,你将使用 MSAL.Net 获取具有以下作用域的 Fabric 服务的 Entra ID 令牌:Workspace.ReadWrite.All、Item.ReadWrite.All。

有关使用 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

在本节中,你将:

  1. 使用我们在上一部分中获取的令牌创建 C# HttpClient。
  2. 添加 https://api.fabric.microsoft.com/v1/ 为客户端的基 URL。
  3. 调用列表工作区 API 并将响应写入控制台。

创建 http 客户端和调用列表工作区 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