We had a console application which is run by Windows Scheduled task, and it was unable to open a document library.
1. Code was executed on SharePoint 2013 Server
2. Adding Site Collection Admin permissions or even Full control via Web Application policy did not help. However, when we tested to logon to server, user was able to open the document library with browser, so the user had sufficient access.
3. The URL was correct, user was able to open the document library with browser
4. Build Platform was set to Any CPU (prefer 32-bit unchecked). We also tested x64, no effect
5. .NET Framework target was 4.5.2. We also tried to downgrade to 4.5, no effect.
The only thing which seemed to help were these:
# gives access to config db, user is programmatically able to open SPSite-object
Add-SPShellAdmin -username [domain]\[username]
# gives access to content db, user is programmatically able to open SPWeb-object
Add-SPShellAdmin -username [domain]\[username] -database [content db guid]
You can get the Guid of content db by running following
Get-SPContentDatabase -webapplication [web application url]