Eng Soon Cheah
Eng Soon Cheah

Reputation: 257

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

Currently I am trying to connect to SharePoint via Console Application authentication. The error returned is:

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

Upvotes: 0

Views: 615

Answers (1)

LZ_MSFT
LZ_MSFT

Reputation: 4208

Use the SharePointOnlineCredentials to pass the credential.

The following example code for your reference.

static void Main(string[] args)
{

    string targetSiteURL = @"https://xxx.sharepoint.com/sites/lz";

    var login = "[email protected]";
    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();             
}

Upvotes: 1

Related Questions