所以,我在这里使用方法2: https://www.vrdmn.com/2019/01/working-with-application-permissions.html
我的代码如下:
string siteUrl = "https://xx.sharepoint.com/sites/test212/";
string siteUrl2 = "https://xx.sharepoint.com/sites/test2122/";
//Client id and Client secret created through appregnew.aspx and permissions granted through appinv.aspx
var authManager = new OfficeDevPnP.Core.AuthenticationManager();
//PnP Core method
ClientContext clientContext = authManager.GetAppOnlyAuthenticatedContext(siteUrl, "xx", "xx");
var teamNoGroupSiteResult = await clientContext.CreateSiteAsync(new OfficeDevPnP.Core.Sites.TeamNoGroupSiteCollectionCreationInformation()
{
Url = siteUrl,
Title = "Team no group Site Test",
Description = "Site description",
Lcid = 1033
});
var communicationSiteResult = await clientContext.CreateSiteAsync(new OfficeDevPnP.Core.Sites.CommunicationSiteCollectionCreationInformation()
{
Url = siteUrl2,
Title = "Modern no group Site Test",
Description = "Site description",
Lcid
});
因此,我在租户中创建了仅应用程序注册,然后尝试了此代码,并说我未经授权。
我得到的错误是:
{
"Message": "An error has occurred.",
"ExceptionMessage": "Token request failed.",
"ExceptionType": "Microsoft.IdentityModel.SecurityTokenService.RequestFailedException",
"StackTrace": " at SharePointPnP.IdentityModel.Extensions.S2S.Protocols.OAuth2.OAuth2S2SClient.Issue(String securityTokenServiceUrl, OAuth2AccessTokenRequest oauth2Request)\r\n at OfficeDevPnP.Core.Utilities.TokenHelper.GetAppOnlyAccessToken(String targetPrincipalName, String targetHost, String targetRealm)\r\n at OfficeDevPnP.Core.AuthenticationManager.EnsureToken(String siteUrl, String realm, String appId, String appSecret, String acsHostUrl, String globalEndPointPrefix)\r\n at OfficeDevPnP.Core.AuthenticationManager.GetAppOnlyAuthenticatedContext(String siteUrl, String realm, String appId, String appSecret, String acsHostUrl, String globalEndPointPrefix)\r\n at OfficeDevPnP.Core.AuthenticationManager.GetAppOnlyAuthenticatedContext(String siteUrl, String appId, String appSecret)\r\n at LuloWebApi.Controllers.TenantController.<Test>d__1.MoveNext() in C:\\Users\\levalencia30\\Source\\Repos\\LuloBackend\\TenantManagementWebApi\\Controllers\\TenantController.cs:line 42\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__3`1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()",
"InnerException": {
"Message": "An error has occurred.",
"ExceptionMessage": "The remote server returned an error: (401) Unauthorized.",
"ExceptionType": "System.Net.WebException",
"StackTrace": " at System.Net.HttpWebRequest.GetResponse()\r\n at SharePointPnP.IdentityModel.Extensions.S2S.Protocols.OAuth2.OAuth2WebRequest.GetResponse()\r\n at SharePointPnP.IdentityModel.Extensions.S2S.Protocols.OAuth2.OAuth2S2SClient.Issue(String securityTokenServiceUrl, OAuth2AccessTokenRequest oauth2Request)"
}
}
答案 0 :(得分:1)
根据this issue,到目前为止无法使用PnP。尽管在Dev分支中有代码更改来解决它,所以您可以复制它并创建自己的CreateSiteAsync扩展方法,然后将sitecollection.cs复制到您自己的项目中。
无法根据相关CreateAsync方法中的代码自行调用Rest API。
编辑:我永远无法仅使用具有ClientId和秘密的应用程序来工作。尝试概述here
的ClientId和Cert方法答案 1 :(得分:0)
您不能执行该PnP。您可以尝试在dev分支中更改它的代码。这将允许您复制它并创建CreateSiteAsync扩展方法,然后将sitecollction.cs复制到您的项目中。