Anteckning
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
Inledning
Om du vill arbeta med Infrastruktur-API:er, precis som många andra Microsoft-tjänster, måste du först hämta en Microsoft Entra-token för Fabric-tjänsten och sedan använda den token i auktoriseringshuvudet för API-anropet.
I den här snabbstartsguiden skapar du en C#-konsolapp som hämtar en Entra-ID-token med hjälp av MSAL.Net-biblioteket och sedan använder C# HttpClient för att anropa API för listarbetsytor.
Skapa appregistrering
För att få en Microsoft Entra-token måste du först registrera ett program med Microsoft Entra-ID.
Mer information om hur du registrerar ett program och olika egenskaper för programmet och hur det kan vara tillämpligt för ditt scenario finns i Registrera en app på Microsofts identitetsplattform.
I den här snabbstartsguiden skapar du en offentlig klient med omdirigerings-URI = http://localhost
Logga in på administrationscentret för Microsoft Entra som minst en molnprogramadministratör.
Bläddra till Program > Appregistreringar.
Klicka på Ny registrering.
Ange ett visningsnamn för programmet och lägg till omdirigerings-URI för offentlig klient
http://localhost
Välj Registrera.
Kopiera program-ID:t (klient) och klistra in det i ett anteckningsblock som ska användas senare.
Hämta token
I den här handledningen använder du MSAL.Net för att skaffa en Entra ID-token för Fabric-tjänsten med följande omfång: Workspace.ReadWrite.All, Item.ReadWrite.All.
Mer information om hur man förvärvar token med MSAL.Net finns i Tokenförvärv – Microsoft Authentication Library för .NET.
Klistra in det program-ID (klient)-ID som du kopierade tidigare och klistra in det för ClientId-variabeln.
C#-kodexempel för att hämta en Microsoft Entra-åtkomsttoken
#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 för samtalslista för arbetsytor
I det här avsnittet kommer du att:
- Skapa C# HttpClient med den token som vi hämtade i föregående avsnitt.
- Lägg till
https://api.fabric.microsoft.com/v1/som bas-URL för klienten. - Anropa LIST-arbetsytor-API:et och Skriv svaret till konsolen.
C#-kodexempel på hur du skapar en HTTP-klient och anropar API för listarbetsytor
// 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);
Fullständigt kodexempel för C#-konsolen
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