azure AD 中的范围与应用程序中的范围不同(Microsoft Graph Api)

时间:2021-06-15 10:23:29

标签: c# azure-active-directory microsoft-graph-api azure-ad-graph-api

我正在尝试使用 OneDrive api 访问 Microsoft Graph 中的文件,

在我的 Azure AD 中,我授予了以下权限:enter image description here

在我的应用程序中,我有以下代码: enter image description here

但是,当我运行应用程序时,它给了我以下错误:

Error getting access token: AADSTS70011: The provided value for the input parameter 'scope' is not valid. One or more scopes in 'https://graph.microsoft.com/.default offline_access profile openid' are not compatible with each other. 即使我没有在其他任何地方授予 offline_access profile openid 权限。

即使我将 _scopes 更改为 private string[] _scopes = new string[4]{"User.Read","Files.ReadWriteAll.All","Files.Read","Files.ReadWrite.AppFolder"},我仍然会遇到相同的错误,但 "https://graph.microsoft.com/.default" 将被替换为上述值。

1 个答案:

答案 0 :(得分:1)

根据您提供的信息,您使用的范围不正确。应该是 private string[] _scopes = new string[4]{"User.Read","Files.ReadWrite.All","Files.Read","Files.ReadWrite.AppFolder"}

例如

  1. 注册 Azure AD 应用程序

  2. 启用设备代码流 enter image description here

  3. 代码


            var client = PublicClientApplicationBuilder
                .Create("")
                .WithAuthority( AadAuthorityAudience.AzureAdMyOrg)
                .WithTenantId("consumers")
                .Build();

            var scopes = new string[] { "User.Read", "Files.ReadWrite.All" , "Files.ReadWrite.AppFolder", "Files.Read" };
           await  client.AcquireTokenWithDeviceCode(scopes, deviceCodeResult =>
            {
                Console.WriteLine(deviceCodeResult.Message);
                return Task.FromResult(0);

            }).ExecuteAsync();

enter image description here