c# - 尝试使用 GraphAPI 访问驱动器项目时出现“租户没有 SPO 许可证”
问题描述
我正在尝试了解如何配置 dotnet core Web 应用程序以让我使用个人 Microsoft 帐户登录并浏览我的 OneDrive 文件。
首先,我使用了 Microsoft Graph Explorer,使用我想使用的帐户登录,并验证我可以浏览我的驱动器。它告诉我查询 URL 是https://graph.microsoft.com/v1.0/me/drive/root/children并且它使用的是 Graph API 的 v1.0。甚至还有 ac# 代码片段选项卡向我展示了这一点:
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var children = await graphClient.Me.Drive.Root.Children
.Request()
.GetAsync();
因此,在我的 dotnet 核心网络应用程序中,我完成了以下操作。在Startup
:
string[] initialScopes = Configuration.GetValue<string>("GraphApi:Scopes")?.Split(' ');
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration, "AzureAd")
.EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
.AddMicrosoftGraph(Configuration.GetSection("GraphApi"))
.AddInMemoryTokenCaches();
在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"
}
和一个测试控制器:
[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
});
}
}
对于客户端凭据,我在 Azure 门户中创建了一个应用注册,其中包含 Graph API 对等权限user.read
并files.read.all
设置,并添加了一个客户端密码。我不太确定的身份验证部分。即使我使用相同的个人 Microsoft 帐户访问 Azure Portal 和 OneDrive,这也不是企业场景,因此我选择了多租户访问。
当我运行这个应用程序并浏览到由我的测试控制器处理的路由时,我被重定向到我登录的 Microsoft OAuth 流。回到应用程序中,该行 _graphServiceClient.Me.Request().GetAsync()
返回我刚刚登录的帐户的用户配置文件。但是,下一行 ( _graphServiceClient.Me.Drive.Root.Children.Request().GetAsync()
) 会引发异常:
ServiceException:代码:BadRequest 消息:租户没有 SPO 许可证
我的理解是 SPO 是指 SharePoint Online,这表明此请求需要企业许可证。但是,我已经通过 Graph Explorer 确认它应该是可能的。
我确实尝试了其他身份验证选项 - 仅限个人帐户和所有帐户 - 但在这两种情况下我都收到了不同的错误:
“'xxxxxxxxxxx'(AppName) 配置为仅供 Microsoft 帐户用户使用。请使用 /consumers 端点来处理此请求”
我没有在网上找到任何解释这个问题的帮助,所以请寻求建议。
解决方案
好吧,让我解释一下你的疑惑。
首先,您误解了我在评论中所说的内容。我只是问您是否将支持帐户类型设置为:Personal Microsoft accounts only
创建应用程序时。我只是要求您验证,而不是建议您这样做。因为这可能是错误的原因:'xxxxxxxxxxx'(AppName) is configured for use by Microsoft Account users only. Please use the /consumers endpoint to serve this request
。
根据文档。/tenant id
or/contoso.onmicrosoft.com
终结点仅允许具有特定 Azure AD 租户的工作/学校帐户的用户登录到应用程序。它不支持个人帐户。
只有/common
和/consumers
端点将允许个人 Microsoft 帐户登录到应用程序。因为只有使用个人账号才能得到正确的响应,所以只需要使用/common
端点登录即可。
推荐阅读
- javascript - 桑基图节点从低到高排序
- erlang - 二叉树的 Erlang 通用 foldl/3 等效项
- python - -bash: image-scraper: 找不到命令
- python - Python - selenium - 无法获取 xpath
- linux - linux内核中的parse_early_parm函数
- reactjs - 使用 React Hooks 设置状态后如何访问状态?
- selenium - 迁移到 WSL 后,Selenium 不再工作。由于 SessionNotCreatedException
- c - 同一程序的不同变体中没有性能差异
- spring-boot - Spring Boot 配置属性验证不起作用
- linux - 从脚本创建 sudo 用户?