我是一个POC,我必须从c#连接到Fabric Rest API,我想创建Item.我可以按照https://learn.microsoft.com/en-us/rest/api/fabric/articles/get-started/fabric-api-quickstart上提供的指示创建项目. 我面临的挑战是创造 token .根据此页面上提供的说明,我将获得一个登录页面,并在成功验证后,生成令牌. 我将在api内部使用令牌创建调用,但我没有进行身份验证的规定.如果可能的话,我需要一些关于如何使用客户端密钥生成令牌的指导.我目前使用的代码如下:

using Microsoft.Identity.Client;
#region parameters section 
string ClientId = "clientID";
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

我已经在Azure active directory中创建了一个新的应用注册,并为Power Bi和Azure存储添加了api权限.我还提供了管理员同意新添加的权限.我创建了一个客户机密,并试图在postman 中创建一个令牌.创建了令牌,但当使用该令牌时,它给了我一个错误的请求.当使用从c#代码生成的令牌时,它的工作正常.

推荐答案

最初,我注册了一个Entra ID应用程序,并在其中创建了一个client secret:

enter image description here

您需要将上述服务主体添加到Fabric子中,至少提供Contributor个角色,如下所示:

enter image description here

在生成令牌之前,请确保通过在Admin Portal中启用以下选项允许访问service principals以使用Fabric API:

enter image description here

现在,您可以通过Postman使用client credentials flow生成访问令牌,参数如下:

POST https://login.microsoftonline.com/tenantId/oauth2/v2.0/token
grant_type:client_credentials
client_id:appId
client_secret:secret
scope: https://api.fabric.microsoft.com/.default

Response:

enter image description here

当我使用这个令牌通过调用Fabric API来列出NTFS时,我成功地得到了response个,如下所示:

GET https://api.fabric.microsoft.com/v1/workspaces/

Response:

enter image description here

要通过生成带有客户端凭证流的令牌来在c#中获得相同的response,可以使用下面的示例代码:

#region
using Microsoft.Identity.Client;
using System.Net.Http.Headers;

string ClientId = "appId";
string ClientSecret = "secret"; 
string Authority = "https://login.microsoftonline.com/tenantId";
#endregion

#region 
string[] scopes = new string[] { "https://api.fabric.microsoft.com/.default" };

ConfidentialClientApplicationBuilder confidentialClientAppBuilder =
    ConfidentialClientApplicationBuilder.Create(ClientId)
    .WithClientSecret(ClientSecret)
    .WithAuthority(Authority);

IConfidentialClientApplication confidentialClientApplication = confidentialClientAppBuilder.Build();

AuthenticationResult result = await confidentialClientApplication.AcquireTokenForClient(scopes)
    .ExecuteAsync()
    .ConfigureAwait(false);

Console.WriteLine(result.AccessToken);
Console.WriteLine();
#endregion


// 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);

Response:

enter image description here

UPDATE:

当我试图从服务主体does not至少有Contributor个访问权限的工作区获取项目时,我也得到了同样的错误:

GET https://api.fabric.microsoft.com/v1/workspaces/{workspaceId}/items/{itemId}

Response:

enter image description here

若要解决此错误,请确保至少授予服务主体Contributor个访问权限,您在其中收到错误:

enter image description here

当我在授予访问权限后运行调用时,我能够通过调用以下API成功获取项目:

GET https://api.fabric.microsoft.com/v1/workspaces/{workspaceId}/items/{itemId}

Response:

enter image description here

但对于其他一些操作,如工作区中的creating items,等等...服务主体认证(客户端证书流)不是supported.

当我try 用service principal令牌在workspace下创建项目时,我得到了error,表示"The operation is not supported for the principal type",如下所示:

POST https://api.fabric.microsoft.com/v1/workspaces/{workspaceId}/items/
{
    "displayName": "Item 1",
    "type": "Lakehouse" 
}

Response:

enter image description here

Reference:. Microsoft Fabric REST APIs | Microsoft.

Csharp相关问答推荐

将实例绑定方法传递到列表foreach

无法更改或使用C#(WinForms.NET)中的全局变量

无法将blob发送到Azure -缺少HTTP标头异常

如何在C#中删除一个特殊字符,如"使用Regex"

如何阻止注释被包含在C#release build. exe中

如何使用XmlSerializer反序列化字符串数组?

在FilePath中搜索一个词,并根据First Match从左到右提取文件路径

try 链接被委派者(多播委托)时,无法将获取运算符应用于类型为';方法组&39;和方法组';的操作数

如何更改新创建的实例的变量?

WinUI 3中DoubleCollection崩溃应用程序类型的依赖属性

如何实现有条件的自定义Json转换器隐藏属性

WPF动态设置弹出窗口水平偏移

如何正确处置所有动态控件?

未显示详细信息的弹出对话框

C#;AvaloniaUI;MVVM;当另一个窗口上的按钮被单击时,如何更新视图图像源?

将列表转换为带有逗号分隔字符串形式的值的字典

使用postman 测试配置了身份的.NET 6应用程序

在Visual Studio 2022中查找Xamarin模板时遇到问题

如何将行添加到DataGrid以立即显示它?

在C#中删除多个不同名称的会话