I am writing a C# console application that lists and then downloads files from a customer's SharePoint site. When I try to connect to their SharePoint site, I get a 403 Forbidden error. The same code works find when I connect to one of my company's SharePoint site.
Both sites use the same AD/Office 365 credentials (myusername@mycompany.com). I can navigate to the customer's SharePoint site in a browser and am able to view and download the files there using those credentials. But when I try to do the same thing via code, I get the 403 error.
Here is the code I am using:
using (ClientContext context = new ClientContext("https://customer.sharepoint.com/sites/SiteToReadFilesFrom/"))
{
context.AuthenticationMode = ClientAuthenticationMode.Default;
string account = "myusername@mycompany.com";
string password = "mypassword";
var secret = new SecureString();
foreach (char c in password)
{
secret.AppendChar(c);
}
context.Credentials = new SharePointOnlineCredentials(account, secret);
context.Load(context.Web);
context.ExecuteQuery(); <!-- throws the error
}
If I switch the URL in the ClientContext to one of my SharePoint sites, it works fine. And I have access to the site and can view/download via a web browser (or Teams - this is a SharePoint site attached to a Team that I was added to).
I feel like I'm missing something obvious here. Is there a way to specify which AD Directory I am authenticating against? I have no control over the customer's site (or AD for that matter) so I have to solve this on my side unless is is a very simple request to change something on their side (like permissions).
Any and all help would be greatly appreciated.