【问题标题】:Is It Possible To Access SharePoint Online Data Using Azure AD Authentication?是否可以使用 Azure AD 身份验证访问 SharePoint Online 数据?
【发布时间】:2018-05-25 05:25:28
【问题描述】:

我有一个托管在 Azure 应用服务上的自定义安全 API (API1) 和一个可以访问自定义 API (API1)。

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

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

【问题讨论】:

    标签: azure azure-active-directory sharepoint-online


    【解决方案1】:

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

    如何:

    1. Integrate your App with AAD.

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

    3. 为您的应用选择必要的权限。

    【讨论】:

      【解决方案2】:

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

      我遵循相同的步骤并使用了以下代码,现在我可以使用 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 的访问令牌,我使用了下面文章中的代码。

      active-directory-dotnet-native-headless

      以下是我遵循的步骤:

      1. 按照文章中提到的步骤进行

      2. 为应用添加了 Office 365 Sharepoint Online API 访问权限。

      3. 为应用选择了必要的权限。

      4. 并使用上述代码从 SPO SIte 检索数据。

      【讨论】:

        猜你喜欢
        • 2021-10-15
        • 2019-08-02
        • 2020-07-04
        • 2020-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-02
        相关资源
        最近更新 更多