简体   繁体   English

尝试使用 GraphAPI 访问驱动器项目时出现“租户没有 SPO 许可证”

[英]“Tenant does not have a SPO license” when trying to access drive items with GraphAPI

I'm trying to understand how to configure a dotnet core web app to let me sign in using a personal Microsoft account and browse my OneDrive files.我正在尝试了解如何配置 dotnet core web 应用程序,以让我使用个人 Microsoft 帐户登录并浏览我的 OneDrive 文件。

Firstly, I've used the Microsoft Graph Explorer , signed in using the account I want to use, and verified that I can browse my drive.首先,我使用了 Microsoft Graph Explorer ,使用我想使用的帐户登录,并验证我可以浏览我的驱动器。 There it tells me the query URL is https://graph.microsoft.com/v1.0/me/drive/root/children and that it's using v1.0 of the Graph API.在那里它告诉我查询 URL 是https://graph.microsoft.com/v1.0/me/drive/root/children并且它使用图表 ZDB974238714CA8DE1DFZACE.7 的 v1.0。 There's even a c# code snippet tab that shows me this:甚至还有一个 c# 代码片段选项卡向我展示了这个:

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var children = await graphClient.Me.Drive.Root.Children
    .Request()
    .GetAsync();

So, in my dotnet core web app I've done the following.因此,在我的 dotnet core web 应用程序中,我完成了以下操作。 In Startup :Startup中:

    string[] initialScopes = Configuration.GetValue<string>("GraphApi:Scopes")?.Split(' ');
        
        services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
            .AddMicrosoftIdentityWebApp(Configuration, "AzureAd")
            .EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
            .AddMicrosoftGraph(Configuration.GetSection("GraphApi"))
            .AddInMemoryTokenCaches();

In appsettings.json :appsettings.json 中

  "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "<my-domain>.onmicrosoft.com",
    "TenantId": "<tenant-guid-identifier>",
    "ClientId": "<client-id>",
    "ClientSecret": "<client-secret>",
    "CallbackPath": "/signin-oidc"
  },
  "GraphApi": {
    "BaseUrl": "https://graph.microsoft.com/v1.0",
    "Scopes": "user.read files.read.all"
  }

And a test controller:和一个测试 controller:

    [Authorize]
    public class OneDriveController : Controller
    {
        private readonly GraphServiceClient _graphServiceClient;

        public OneDriveController(GraphServiceClient graphServiceClient)
        {
            _graphServiceClient = graphServiceClient;
        }

        [AuthorizeForScopes(ScopeKeySection = "GraphApi:Scopes")]
        public async Task<IActionResult> Index()
        {
            var user = await _graphServiceClient.Me.Request().GetAsync();

            var driveItems = await _graphServiceClient.Me.Drive.Root.Children.Request().GetAsync();

            return Json(new
            {
                user = user.DisplayName,
                items = driveItems.Count
            });
        }
    }

For the client credentials I've created an app registration in my Azure portal with Graph API peermissions user.read and files.read.all set, and added a client secret.对于客户端凭据,我在我的 Azure 门户中创建了一个应用注册,其中设置了 Graph API peermissions user.readfiles.read.all ,并添加了一个客户端密码。 The authentication part I'm not so sure about.我不太确定的身份验证部分。 Even though I use the same personal Microsoft account to access Azure Portal and OneDrive this isn't an enterprise scenario so I opted for multitenant access.即使我使用同一个个人 Microsoft 帐户访问 Azure 门户和 OneDrive,这也不是企业场景,因此我选择了多租户访问。

When I run this app and browse to the route handled by my test controller I am redirected to the Microsoft OAuth flow where I sign in. Back in the app the line _graphServiceClient.Me.Request().GetAsync() returns the user profile of the account I just signed in with.当我运行此应用程序并浏览到由我的测试 controller 处理的路线时,我被重定向到我登录的 Microsoft OAuth 流。返回应用程序中的_graphServiceClient.Me.Request().GetAsync()行返回用户配置文件我刚刚登录的帐户。 However, the next line ( _graphServiceClient.Me.Drive.Root.Children.Request().GetAsync() ) throws an exception:但是,下一行( _graphServiceClient.Me.Drive.Root.Children.Request().GetAsync() )会引发异常:

ServiceException: Code: BadRequest Message: Tenant does not have a SPO license ServiceException:代码:BadRequest 消息:租户没有 SPO 许可证

My understanding is that SPO means SharePoint Online and this is suggesting that an enterprise license is required for this request.我的理解是 SPO 表示 SharePoint Online,这表明此请求需要企业许可证。 However, I've already confirmed via Graph Explorer that it should be possible.但是,我已经通过 Graph Explorer 确认它应该是可能的。

I did try the other authentication options - personal accounts only and all accounts - but in both cases I received a different error:我确实尝试了其他身份验证选项 - 仅限个人帐户和所有帐户 - 但在这两种情况下我都收到了不同的错误:

"'xxxxxxxxxxx'(AppName) is configured for use by Microsoft Account users only. Please use the /consumers endpoint to serve this request" “'xxxxxxxxxxx'(AppName) 配置为仅供 Microsoft 帐户用户使用。请使用 /consumers 端点来处理此请求”

I haven't found any help online that explains this issue so seeking advice please.我没有在网上找到任何解释这个问题的帮助,所以请寻求建议。

Okay, let me explain your doubts.好吧,让我解释一下你的疑惑。

First of all, you misunderstood what I said in the comments.首先,您误解了我在评论中所说的内容。 I'm just asking if you set the support account type to: Personal Microsoft accounts only when creating the application.我只是问您是否将支持帐户类型设置为: Personal Microsoft accounts only I just ask you for verification, not suggest you do it.我只是要求您验证,而不是建议您这样做。 Because this may be the cause of the error: 'xxxxxxxxxxx'(AppName) is configured for use by Microsoft Account users only. Please use the /consumers endpoint to serve this request因为这可能是错误的原因: 'xxxxxxxxxxx'(AppName) is configured for use by Microsoft Account users only. Please use the /consumers endpoint to serve this request 'xxxxxxxxxxx'(AppName) is configured for use by Microsoft Account users only. Please use the /consumers endpoint to serve this request . 'xxxxxxxxxxx'(AppName) is configured for use by Microsoft Account users only. Please use the /consumers endpoint to serve this request

According to the documentation .根据文档 The /tenant id or /contoso.onmicrosoft.com endpoint only allows users with work/school accounts of a specific Azure AD tenant to log in to the application. /tenant id/contoso.onmicrosoft.com终结点仅允许具有特定 Azure AD 租户的工作/学校帐户的用户登录到应用程序。 It does not support personal accounts.它不支持个人帐户。

Only the /common and /consumers endpoints will allow personal Microsoft accounts to log in to the application.只有/common/consumers端点将允许个人 Microsoft 帐户登录到应用程序。 Because you can get the correct response only by using your personal account, so you only need to log in using the /common endpoint.因为只有使用个人账号才能得到正确的响应,所以只需要使用/common端点登录即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 GraphAPI 时“租户 guid 的租户不存在” - 即使用户类型为成员 - "The tenant for tenant guid does not exist" when using GraphAPI - Even with user type as Member 尝试访问映射的驱动器时,服务内的文件复制失败 - File copy fails within a service when trying to access a mapped drive 尝试访问 Google Drive 时服务器超时,在本地运行时有效 - Server time-out when trying to access Google Drive, works when running locally ActiveReports 7 - 应用程序许可证是否到期? 如果是的话,何时? - ActiveReports 7 - Does an application license expire? If so, when? 尝试通过代码将文本文件写入C:\\ Drive时访问被拒绝异常 - Access Denied Exception when trying to write text file to C:\ Drive through Code behind 尝试将用户添加到演示租户时出现异常 (C#) - Exception when trying to add users to demo tenant (C#) 通过MS GraphAPI的SharePoint,可以访问网站,驱动器为null - SharePoint thru MS GraphAPI, can get to site, drive is null SSIS Visual C# 访问 SPO 列表的脚本组件 - SSIS Visual C# Script Component to access SPO List 以用户身份运行时访问映射驱动器 - Access mapped drive when running as user 尝试访问网络驱动器后引发异常 - Exception thrown after trying to get access to a network drive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM