'C# Sharepoint Library access removal error

I need to remove all the unique permissions in Library level example "Documents"

path:https://sites.xxx.com/sites/11111/_layouts/15/User.aspx?List=

The following code throws error.

Could you please help me?

class Program
    {
        static void Main(string[] args)
        {
            ClientContext ctx = new ClientContext(<siteUrl>);
            var username = "Rajkumar P"; // This user needs to be removed
            Web wsite = ctx.Web;
            ctx.Load(wsite, w => w.HasUniqueRoleAssignments, w => w.RoleAssignments.Include(roleAssigned => roleAssigned.Member.Title,
                roleAssigned => roleAssigned.RoleDefinitionBindings.Include(roleDef => roleDef.Name)));
            ctx.ExecuteQuery();
            RoleAssignment ra = wsite.RoleAssignments.Where(r => r.Member.Title.Equals(username)).FirstOrDefault();
            ra.DeleteObject();
            wsite.Update();

            SecureString secureString = new SecureString();
            string password = "password";
            foreach (char c in password.ToCharArray()) secureString.AppendChar(c);

            NetworkCredential myCred = new NetworkCredential(
                    "<userntid>", secureString,"<FQDN>");
            ctx.Credentials = myCred;
            ctx.ExecuteQuery();

        }
    }

Error:

Exception thrown: 'Microsoft.SharePoint.Client.ServerUnauthorizedAccessException' in Microsoft.SharePoint.Client.Runtime.dll
An unhandled exception of type 'Microsoft.SharePoint.Client.ServerUnauthorizedAccessException' occurred in Microsoft.SharePoint.Client.Runtime.dll
Access denied. You do not have permission to perform this action or access this resource.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source