Sharepoint Online Error:The remote server returned an error: (401) Unauthorized

Sharepoint Online Error:The remote server returned an error: (401) Unauthorized

目前我正在尝试通过控制台应用程序身份验证连接到 SharePoint。 返回的错误是:

The remote server returned an error: (401) Unauthorized.

使用 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();             
}