简体   繁体   中英

Is It Possible To Access SharePoint Online Data Using Azure AD Authentication?

I have a custom Secure API ( API1 ) hosted on Azure App Service & a desktop application( D1 ) which can access the custom API ( API1 ).

Now, we have a requirement to access the SharePoint Online data from our custom API ( API1 ).

Is this feasible, to access SharePoint online data from custom API hosted on Azure App Service using same desktop application ( D1 ) ?

Of course you can! But the the requirement is that you have Sharepoint Online subscription in your Directory.

How to :

  1. Integrate your App with AAD .

  2. Add Office 365 Sharepoint Online API access permissions for your App.

  3. Select necessary permissions for your App.

在此处输入图片说明

Thanks Yang , your suggestion was useful.

I have followed the same steps and used the below code and now I am able to get the data from SPO Site using Azure Token.

 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
    }

Please Note:

To Get the Access Token for Azure , I have used code from below article.

active-directory-dotnet-native-headless

Below are the steps I have followed:

  1. Followed the steps mentioned in the article

  2. Added Office 365 Sharepoint Online API access permissions for the App.

  3. Selected necessary permissions for the App.

  4. And used the code mentioned above to retrieve data from SPO SIte.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM