首页 > 解决方案 > SharePoint csom ADAL 令牌 401 未经授权

问题描述

我生成了一个具有以下权限的 AAD 令牌。

AllSites.FullControl AllSites.Manage AllSites.Read 
AllSites.Write email Mail.Read Mail.Send MyFiles.Read 
MyFiles.Write openid profile Sites.Search.All TermStore.Read.All 
TermStore.ReadWrite.All User.Read User.Read.All User.ReadBasic.All User.ReadWrite.All

使用 nuget SharePointPnPCoreOnline 时出现 401 Unauthorized,我也尝试了 TTCUE.NEtCore.SharepointOnline.CSOM。

我创建了一个分配了各种权限的 AAD 应用程序注册。权限适用于 Mail.Read,但不适用于 SharePoint。

using (var context = new ClientContext("URL"))
        {
            context.AuthenticationMode = ClientAuthenticationMode.Anonymous;
            context.FormDigestHandlingEnabled = false;
            context.ExecutingWebRequest +=
                delegate (object oSender, WebRequestEventArgs webRequestEventArgs)
                {
                    webRequestEventArgs.WebRequest.Headers["Authorization"] =
                        "Bearer " + token;
                };
            context.Load(context.Web);
            context.ExecuteQueryAsync().GetAwaiter().GetResult();
            Console.WriteLine(context.Web.Title);
            Console.ReadLine();
        }

标签: sharepointsharepoint-online

解决方案


我已经找到答案了。SharePoint CSOM 访问需要 Azure Active Directory V1 令牌。确保您没有尝试使用 V2 令牌进行访问。


推荐阅读