使用 Office Pnp Sites Core 使用 App-Only 创建新式网站时出现 401

401 when creating a modern site using App-Only using Office Pnp Sites Core

所以,我在这里使用方法 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)"
      }
    }

根据 this issue,目前尚无法使用 PnP。尽管在 Dev 分支中有代码更改来解决它,因此您可以复制它并创建您自己的 CreateSiteAsync 扩展方法并将 sitecollection.cs 复制到您自己的项目中。

无法根据相关 CreateAsync 方法中的代码自行调用 Rest API。

编辑:我永远无法让仅具有 ClientId 和机密的应用程序运行。试用概述的 ClientId 和 Cert 方法 here

你不能那样做即插即用。您可以尝试在 dev 分支中更改它的代码。这将允许您复制它并制作一个 CreateSiteAsync 扩展方法,然后只需将 sitecollction.cs 复制到您的项目。