Microsoft Graph API Xamarin 应用程序中的范围

Microsoft Graph API Scopes in a Xamarin application

我正在尝试在 Xamarin 应用程序中获取我所属的组。

现在,我正在使用此方法获取令牌:

    public async Task SignIn()
    {
        if (App.IdentityClientApp != null)
            this.authenticationResult = await App.IdentityClientApp.AcquireTokenAsync(new String[] { Constants.ApplicationID });
    }

我的应用程序 ID 来自 Apps.dev.microsoft.com 注册门户。

然后我用这个方法悄悄获取另一个令牌:

    private async Task GetTokenForGroups()
    {
        this.authenticationResult = await App.IdentityClientApp.AcquireTokenAsync(new String[] { "User.Read", "Directory.AccessAsUser.All" }, null);
    }

如果我这样做,系统会提示我使用管理员帐户登录。 如果我这样做,令牌获取将成功,但不是 "remembering" 其他非管理员用户的授权,而是每次都会提示我获得管理员授权。

如果我只使用 "User.Read" 范围进行调用:

    public void GetAuthenticatedClient()
    {
        try
        {
            this.graphClient = new GraphServiceClient(
                "https://graph.microsoft.com/v1.0",
                new DelegateAuthenticationProvider(
                    async (requestMessage) =>
                    {
                        await GetTokenForGroups();
                        requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", this.authenticationResult.Token);
                    }));
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Could not create a graph client: " + ex.Message + ex.InnerException);
        }
    }

    public async void GetGroups()
    {
        this.GetAuthenticatedClient();
        var groups = await graphClient.Me.MemberOf.Request().GetAsync();  
    }

我得到了一些组,但它们似乎是 Outlook 组,而不是安全组。

apps.dev.microsoft.com 中的 My Graph 权限完全为空。

我怎样才能读取目录数据?我做错了什么?

谢谢。

您需要使用管理员同意工作流程。仅以管理员身份登录并不表示同意非管理员用户。有一个特定的工作流程可以实现这一点。

看看我写的一篇文章v2 Endpoint and Admin Consent。它应该为您提供连接同意流程的起点。