是否可以使用Azure AD身份验证访问SharePoint在线数据?

是否可以使用Azure AD身份验证访问SharePoint在线数据?,azure,azure-active-directory,sharepoint-online,Azure,Azure Active Directory,Sharepoint Online,我有一个自定义安全API(API1)托管在Azure应用程序服务上,这是一个桌面应用程序(D1),可以访问自定义API(API1) 现在,我们需要从自定义API(API1)访问SharePoint Online数据 使用相同的桌面应用程序(D1)从Azure App Service上托管的自定义API访问SharePoint online数据是否可行?当然可以!但要求是您的目录中必须有Sharepoint Online订阅 如何: 为您的应用添加Office 365 Sharepoint On

我有一个自定义安全API(API1)托管在Azure应用程序服务上,这是一个桌面应用程序(D1),可以访问自定义API(API1

现在,我们需要从自定义API(API1)访问SharePoint Online数据


使用相同的桌面应用程序(D1)从Azure App Service上托管的自定义API访问SharePoint online数据是否可行?

当然可以!但要求是您的目录中必须有Sharepoint Online订阅

如何:

  • 为您的应用添加Office 365 Sharepoint Online API访问权限

  • 为你的应用选择必要的权限


  • 谢谢杨,你的建议很有用

    我已经按照相同的步骤使用了下面的代码,现在我能够使用Azure令牌从SPO站点获取数据

     static void ConnectToSPO()
        {
            string SiteURL = "https://SPOSite.sharepoint.com/";
    
            #region Obtain token
            AuthenticationResult result = null;
            // first, try to get a token silently
            try
            {
                result = authContext.AcquireTokenSilentAsync(SiteURL, clientId).Result;
            }
            catch (AggregateException exc)
            {
                AdalException ex = exc.InnerException as AdalException;
    
                // There is no token in the cache; prompt the user to sign-in.
                if (ex != null && ex.ErrorCode != "failed_to_acquire_token_silently")
                {
                    // An unexpected error occurred.
                    ShowError(ex);
                    return;
                }
            }
    
            if (result == null)
            {
                UserCredential uc = TextualPrompt();
                // if you want to use Windows integrated auth, comment the line above and uncomment the one below
                // UserCredential uc = new UserCredential();
                try
                {
                    result = authContext.AcquireTokenAsync(todoListResourceId, clientId, uc).Result;
                }
                catch (Exception ee)
                {
                    ShowError(ee);
                    return;
                }
            }
    
            #endregion
    
            #region Get SharePoint Online Context & Access SPO Data
    
            using (ClientContext ctx = TokenHelper.GetClientContextWithAccessToken(SiteURL, result.AccessToken))
            {
    
                try
                {
    
                    Console.ForegroundColor = ConsoleColor.Yellow;
    
                    Console.WriteLine("");
                    Console.WriteLine("*****************************************************************************");
                    Console.WriteLine("Connecting To SPO Site: " + SiteURL);
    
                    ctx.Load(ctx.Web);
                    ctx.ExecuteQuery();
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Connected !");
    
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("Info: Site Name-> " + ctx.Web.Title);
    
                    ctx.Load(ctx.Web.CurrentUser);
                    ctx.ExecuteQuery();
                    Console.WriteLine("Info: Current User Login Name-> " + ctx.Web.CurrentUser.LoginName);
    
                    #region Read List Items
                    Console.WriteLine("");
                    Console.WriteLine("Info: Reading list items from list Test List");
    
                    List testlist = ctx.Web.Lists.GetByTitle("Test List");
                    CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
                    ListItemCollection items = testlist.GetItems(query);
                    ctx.Load(items);
                    ctx.ExecuteQuery();
                    foreach (ListItem listItem in items)
                    {
                        // We have all the list item data. For example, Title. 
                        Console.WriteLine(listItem["Title"]);
                    }
    
                    Console.WriteLine("");
                    #endregion
                }
                catch (Exception ex)
                {
                    ShowError(ex);
                }
            } 
            #endregion
        }
    
    请注意:

    为了获得Azure的访问令牌,我使用了下面文章中的代码

    以下是我遵循的步骤:

  • 遵循文章中提到的步骤

  • 添加了应用程序的Office 365 Sharepoint Online API访问权限

  • 为应用程序选择了必要的权限

  • 并使用上述代码从SPO站点检索数据