目前,我可以使用下面给出的用户ID和密码访问sharepoint列表。但是想了解我如何使用客户端ID和客户端密钥访问列表?
string siteUrl = "https://xyz.sharepoint.com/sites/MyList/";
ClientContext clientContext = new ClientContext(siteUrl);
string username = ConfigurationManager.AppSettings["username"];
string password = ConfigurationManager.AppSettings["password"];
System.Security.SecureString passWord = new System.Security.SecureString();
foreach (char c in password.ToCharArray())
{
passWord.AppendChar(c);
}
clientContext.Credentials = new SharePointOnlineCredentials(username, passWord);
Web oWebsite = clientContext.Web;
ListCollection collList = oWebsite.Lists;
clientContext.Load(collList);
clientContext.ExecuteQuery();
答案 0 :(得分:4)
您可以使用PnP CSOM核心的GetAppOnlyAuthenticatedContext
方法。
之后您可以使用以下代码:
string siteUrl = "https://xyz.sharepoint.com/sites/MyList/";
string clientId = "<client-id>";
string clientSecret = "<client-secret>";
using (var clientContext = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl,clientId,clientSecret))
{
Web oWebsite = clientContext.Web;
ListCollection collList = oWebsite.Lists;
clientContext.Load(collList);
clientContext.ExecuteQuery();
}
要添加PnP CSOM核心,请转到项目参考&gt;管理nuget包。
添加SharePointPnPCoreOnline包。
参考文献 - Authenticate SharePoint using PnP Authentication Manager