Sharepoint Online错误:远程服务器返回错误:(401)未经授权

时间:2019-07-03 03:11:45

标签: sharepoint-online

当前,我尝试通过控制台应用程序身份验证连接到SharePoint。 错误返回:

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

感谢进阶。

1 个答案:

答案 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();             
}