none
Using SharePoint Online (365) CSOM Receiving 401 Unauthorized RRS feed

  • Question

  • Hello All

    I'm trying to download from a document library to a network share using .NET and the CSOM package.  When running the code, I'm receiving System.Net.WebException: 'The remote server returned an error: (401) Unauthorized.'

    Here is the call to my local method.

    DownloadFilesFromSharePoint(@"http://site.sharepoint.com/sites/SubSite", @"https://site.sharepoint.com/sites/SubSite/Shared%20Documents/Subfolder", @"X:\MyDestination");

    The below code fails at ctx.ExecuteQuery();

    internal static void DownloadFilesFromSharePoint(string siteUrl, string folderPath, string tempLocation)
            {
                var Username = "me@site.com";
                var Password = "Password01"; //changing password for question
                var securePassword = new SecureString();
                //Convert string to secure string
                foreach (char c in Password)
                {
                    securePassword.AppendChar(c);
                }
                securePassword.MakeReadOnly();
    
    
                ClientContext ctx = new ClientContext(siteUrl);
                
                ctx.Credentials = new SharePointOnlineCredentials(Username, securePassword);
    
                FileCollection files = ctx.Web.GetFolderByServerRelativeUrl(folderPath).Files;
    
                ctx.Load(files);
                ctx.ExecuteQuery();
    
                foreach (Microsoft.SharePoint.Client.File file in files)
                {
                    FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, file.ServerRelativeUrl);
                    ctx.ExecuteQuery();
    
                    var filePath = tempLocation + file.Name;
                    using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
                    {
                        fileInfo.Stream.CopyTo(fileStream);
                    }
                }
            }

    Do I need some type of elevated permissions?

    Wednesday, December 4, 2019 10:06 PM

Answers

  • Hi,

    If you use Multi-Factor Authentication, please modify the authentication code as below.

    var authManager = new AuthenticationManager();
    var ctx = authManager.GetWebLoginClientContext("http://site.sharepoint.com/sites/SubSite");

    More information:

    SharePoint Online: Using CSOM with an account configured with Multi-factor Authentication (MFA)

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by TheMick15a Wednesday, December 11, 2019 10:22 PM
    Wednesday, December 11, 2019 8:10 AM
    Moderator

All replies

  • what kind of permission you have for the user who is running the script ? I think minimum permissions should be Contribute / Edit to run the script to download files.

    Thanks & Regards,


    sharath aluri

    Wednesday, December 4, 2019 10:09 PM
  • The user is a "Site Member".
    Wednesday, December 4, 2019 10:19 PM
  • Hi,

    Please check if the user can download the files from the SharePoint UI.

    And try to use admin user to check if the code works.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, December 5, 2019 3:19 AM
    Moderator
  • Ok, I have made the user an Admin... now I'm getting a different error.

    'The sign-in name or password does not match one in the Microsoft account system.'

    So we are making progress!

    Tuesday, December 10, 2019 7:55 PM
  • This Error states like you are entering wrong credentials, make sure you are entering the right one.

    Thanks & Regards,


    sharath aluri

    Tuesday, December 10, 2019 9:16 PM
  • The credentials are correct (I checked them like 50 times lol), I'm thinking now that this is because of Multi-Factor Authentication.  Each time the user logs into SharePoint they have to use an authentication token as well as the password.
    Tuesday, December 10, 2019 9:36 PM
  • Hi,

    If you use Multi-Factor Authentication, please modify the authentication code as below.

    var authManager = new AuthenticationManager();
    var ctx = authManager.GetWebLoginClientContext("http://site.sharepoint.com/sites/SubSite");

    More information:

    SharePoint Online: Using CSOM with an account configured with Multi-factor Authentication (MFA)

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by TheMick15a Wednesday, December 11, 2019 10:22 PM
    Wednesday, December 11, 2019 8:10 AM
    Moderator