Connect to Sharepoint Online returns 401: Unauthorized

I am trying to search Sharepoint Online using the C # API:

var clientContext =
    new ClientContext("https://foobar.sharepoint.com/_layouts/15/start.aspx#/Shared%20Documents");
var pw = "apassword";
var secure = new SecureString();
foreach (var c in pw) secure.AppendChar(c);

var credentials = new SharePointOnlineCredentials("adress@mail.com", secure);
clientContext.Credentials = credentials;

var keywordQuery = new KeywordQuery(clientContext);
keywordQuery.QueryText = "SharePoint";
var searchExecutor = new SearchExecutor(clientContext);
var results = searchExecutor.ExecuteQuery(keywordQuery);
clientContext.ExecuteQuery();

      

I get a "401: Unauthorized" response even though the authorization I gave is correct. What am I missing here?

+3


source to share


1 answer


You are trying to connect to a SharePoint document library ( https://foobar.sharepoint.com/_layouts/15/start.aspx#/Shared%20Documents ). Try connecting to " https://foobar.sharepoint.com/ ".



Also, the client context may not have sufficient permissions to perform searches.

+2


source







All Articles