当前,我尝试通过控制台应用程序身份验证连接到SharePoint。 错误返回:
The remote server returned an error: (401) Unauthorized.
感谢进阶。
答案 0 :(得分:0)
使用SharePointOnlineCredentials传递凭据。
以下示例代码供您参考。
static void Main(string[] args)
{
string targetSiteURL = @"https://xxx.sharepoint.com/sites/lz";
var login = "lz@xxx.onmicrosoft.com";
var password = "xxx";
var securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
ClientContext ctx = new ClientContext(targetSiteURL);
ctx.Credentials = onlineCredentials;
Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
Console.WriteLine(web.Title);
Console.ReadKey();
}