简体   繁体   中英

How to connect to SharePoint on Office 365 with CSOM from C#?

I'm having trouble connecting to a new dev SharePoint Online site on Office 365. I can view the site and a list from a browser, and I've added an API user as a site admin and an O365 admin. I keep getting a 401 error when calling ClientContext.ExecuteQuery() . Any advice is appreciated.

I don't know whether I'm hitting the wrong URL or the user doesn't have rights, or maybe it's something totally different. I've tested the username and password, as it gives me a different error about an invalid user if I use the wrong password. So, I'm pretty sure that I have the right credentials. The user has been set as an O365 admin and has been granted "Edit" rights to the SharePoint site through that console. I've even tried using my own credentials, and I'm the one that created the site.

My goal is to eventually get list items from a particular list. However, since I can't do this much, I haven't gotten too far.

Here's some sample code from a console app:

ClientContext cc;
SecureString pw;

cc = new ClientContext("http://XXXX.sharepoint.com");
pw = new SecureString();
"XXXX".ToCharArray().ToList().ForEach(c => pw.AppendChar(c));
cc.Credentials = new SharePointOnlineCredentials("api@XXXX.onmicrosoft.com", pw);
cc.Load(cc.Web);
cc.ExecuteQuery();

Try this:

ClientContext cc = new ClientContext("https://XXXX.sharepoint.com");
SecureString pass = new SecureString();

foreach (char c in "XXXX".ToCharArray();
    pass.AppendChar(c);

cc.Credentials = new SharePointOnlineCredentials("api@XXXX.onmicrosoft.com", pass);
cc.Load(cc.Web);
cc.ExecuteQuery();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM