add-pnpapp:远程服务器返回一个错误:(401)未授权

发布于 2025-01-22 18:59:41 字数 4812 浏览 0 评论 0原文

我遵循了本文,以便使用clientId和clientsecret使用Connect-pnPonline:

https://www.sharepointdiary.com/2019/03/connect-pnponline-with-with-appid-anpid-and-appsecret.html

_layouts/15/appregnew.aspx

https://.sharepoint.com/sites/catalog/_layouts/15/appinv.aspx 因此,我能够成功连接。

    # Connect-PnPOnline -Url $SiteURL -ClientId $ClientId -ClientSecret $ClientSecret

    Write-Verbose "Add and publish your app to the App Catalog...."
    Add-PnPApp -Path $Path -Scope Tenant -Overwrite -Publish
    Write-Verbose "Successfully added and published to the App Catalog"   

Add-PnPApp : The remote server returned an error: (401) Unauthorized.
At C:\Scripts\ReleaseManagement\Publish-PnPAppToAppCatalog.ps1:55 char:2
+     Add-PnPApp -Path $Path -Scope Tenant -Overwrite -Publish
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Add-PnPApp], WebException
    + FullyQualifiedErrorId : EXCEPTION,SharePointPnP.PowerShell.Commands.Apps.AddApp

完整脚本:

function Publish-PnPAppToAppCatalog {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$True)]
        [string] $SolutionAbbreviation,
        [Parameter(Mandatory=$True)]
        [string] $EnvironmentAbbreviation,
        [Parameter(Mandatory=$True)]
        [string] $SiteName,
        [Parameter(Mandatory=$True)]
        [string] $Path
        
    )
    
    $scriptsDirectory = Split-Path $PSScriptRoot -Parent

    . ($scriptsDirectory + '\Common\Connect-PnPOnlineForPublishApps.ps1')
    Connect-PnPOnlineForPublishApps -SolutionAbbreviation $SolutionAbbreviation `
                                    -EnvironmentAbbreviation $EnvironmentAbbreviation `
                                    -ConnectionType "tenant" `
                                    -SiteName $SiteName `                                   
                                    -Verbose   

    Write-Verbose "Add and publish your app to the App Catalog...."
    Add-PnPApp -Path $Path -Scope Tenant -Overwrite -Publish
    Write-Verbose "Successfully added and published to the App Catalog"           
   
             
}

Publish-PnPAppToAppCatalog -SolutionAbbreviation "" `
-EnvironmentAbbreviation "" `
-SiteName "" `
-Path "" `
-Verbose
function Connect-PnPOnlineForPublishApps {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$True)]
        [string] $SolutionAbbreviation,
        [Parameter(Mandatory=$True)]
        [string] $EnvironmentAbbreviation,
        [Parameter(Mandatory=$True)]
        [ValidateSet("tenant", "sites")]
        [string] $ConnectionType,
        [Parameter(Mandatory=$False)]
        [string] $SiteName
       

    )
    #Requires -Version 5
    $scriptsDirectory = Split-Path $PSScriptRoot -Parent

    . ($scriptsDirectory + '\Common\Add-AzAccountIfNeeded.ps1')
    Add-AzAccountIfNeeded
    
    . ($scriptsDirectory + '\Common\Install-AzKeyVaultModuleIfNeeded.ps1')
    Install-AzKeyVaultModuleIfNeeded      

    Install-Module PnP.PowerShell -RequiredVersion "1.10.0" -Scope CurrentUser -Force -AllowClobber
   
        
    #Site collection URL
    $SiteURL = "https://<tenant>.sharepoint.com"
 
    #Connect to SharePoint Online with ClientId and ClientSecret
    Connect-PnPOnline -Url $SiteURL -ClientId "<client-id>" -ClientSecret "<client-secret>"
       
    Get-PnPContext

    Set-PnPTenant -DisableCustomAppAuthentication $false

    Write-Verbose "CONNECTED.................."
   

更新:

我根据建议更新了脚本:

Connect-SPOService -Url $url -Credential $credential
Set-SPOTenant -DisableCustomAppAuthentication $false  

#Site collection URL
$SiteURL = "https://<tenant>.sharepoint.com"
 
#Connect to SharePoint Online with ClientId and ClientSecret
Connect-PnPOnline -Url $SiteURL -ClientId "<client-id>" -ClientSecret "<client-secret>"

我看到一个禁止错误:

“在此处输入图像说明”

I followed this article in order to use Connect-PnPOnline using ClientID and ClientSecret:

https://www.sharepointdiary.com/2019/03/connect-pnponline-with-appid-and-appsecret.html

https://.sharepoint.com/sites/catalog/_layouts/15/appregnew.aspx
enter image description here

https://.sharepoint.com/sites/catalog/_layouts/15/appinv.aspx
With that I'm able to successfully connect.

enter image description here

enter image description here

After connecting, I have the following script:

    # Connect-PnPOnline -Url $SiteURL -ClientId $ClientId -ClientSecret $ClientSecret

    Write-Verbose "Add and publish your app to the App Catalog...."
    Add-PnPApp -Path $Path -Scope Tenant -Overwrite -Publish
    Write-Verbose "Successfully added and published to the App Catalog"   

And I see the error:

Add-PnPApp : The remote server returned an error: (401) Unauthorized.
At C:\Scripts\ReleaseManagement\Publish-PnPAppToAppCatalog.ps1:55 char:2
+     Add-PnPApp -Path $Path -Scope Tenant -Overwrite -Publish
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Add-PnPApp], WebException
    + FullyQualifiedErrorId : EXCEPTION,SharePointPnP.PowerShell.Commands.Apps.AddApp

What am I missing?

Full Script:

function Publish-PnPAppToAppCatalog {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$True)]
        [string] $SolutionAbbreviation,
        [Parameter(Mandatory=$True)]
        [string] $EnvironmentAbbreviation,
        [Parameter(Mandatory=$True)]
        [string] $SiteName,
        [Parameter(Mandatory=$True)]
        [string] $Path
        
    )
    
    $scriptsDirectory = Split-Path $PSScriptRoot -Parent

    . ($scriptsDirectory + '\Common\Connect-PnPOnlineForPublishApps.ps1')
    Connect-PnPOnlineForPublishApps -SolutionAbbreviation $SolutionAbbreviation `
                                    -EnvironmentAbbreviation $EnvironmentAbbreviation `
                                    -ConnectionType "tenant" `
                                    -SiteName $SiteName `                                   
                                    -Verbose   

    Write-Verbose "Add and publish your app to the App Catalog...."
    Add-PnPApp -Path $Path -Scope Tenant -Overwrite -Publish
    Write-Verbose "Successfully added and published to the App Catalog"           
   
             
}

Publish-PnPAppToAppCatalog -SolutionAbbreviation "" `
-EnvironmentAbbreviation "" `
-SiteName "" `
-Path "" `
-Verbose
function Connect-PnPOnlineForPublishApps {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$True)]
        [string] $SolutionAbbreviation,
        [Parameter(Mandatory=$True)]
        [string] $EnvironmentAbbreviation,
        [Parameter(Mandatory=$True)]
        [ValidateSet("tenant", "sites")]
        [string] $ConnectionType,
        [Parameter(Mandatory=$False)]
        [string] $SiteName
       

    )
    #Requires -Version 5
    $scriptsDirectory = Split-Path $PSScriptRoot -Parent

    . ($scriptsDirectory + '\Common\Add-AzAccountIfNeeded.ps1')
    Add-AzAccountIfNeeded
    
    . ($scriptsDirectory + '\Common\Install-AzKeyVaultModuleIfNeeded.ps1')
    Install-AzKeyVaultModuleIfNeeded      

    Install-Module PnP.PowerShell -RequiredVersion "1.10.0" -Scope CurrentUser -Force -AllowClobber
   
        
    #Site collection URL
    $SiteURL = "https://<tenant>.sharepoint.com"
 
    #Connect to SharePoint Online with ClientId and ClientSecret
    Connect-PnPOnline -Url $SiteURL -ClientId "<client-id>" -ClientSecret "<client-secret>"
       
    Get-PnPContext

    Set-PnPTenant -DisableCustomAppAuthentication $false

    Write-Verbose "CONNECTED.................."
   

UPDATE:

I updated the script as per the suggestion:

Connect-SPOService -Url $url -Credential $credential
Set-SPOTenant -DisableCustomAppAuthentication $false  

#Site collection URL
$SiteURL = "https://<tenant>.sharepoint.com"
 
#Connect to SharePoint Online with ClientId and ClientSecret
Connect-PnPOnline -Url $SiteURL -ClientId "<client-id>" -ClientSecret "<client-secret>"

I see a Forbidden error:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文