Unable to upload files to sharepoint using PnP.Powershell - constantly getting 401 error

na 146 Reputation points
2024-11-30T11:03:24.6166667+00:00

I am trying to grant an application registration access to upload files to a sharepoint site using PnP Online. All the commands seem to work, but when i try uploading a file i get a 401 error. I would appreciate any expertise on how to make this work. The steps ive followed are::

  1. create app registration
  2. Add sharepoint Sites.FullControl.All api permissions & approve it.
  3. Create self signed certificate, convert it to .cer & upload it.
  4. Connect using Connect-PnPOnline. this returns no errors.
  5. run Grant-PnPAzureADAppSitePermission. Again this returns no errors.
  6. run AddPnPFile. This gives the error 401 Unauthorized.

All code is below along with a screenshot of the output. Appreciate any help on this.

$Sites_FullAccess_App_ID = "19ef4773-433b-46ac-9859-865824293acd"
$Sites_FullAccess_Obj_ID = "6f8b2588-96ed-4a78-ab81-9cda9730a798"
$Sites_FullAccess_Name = "Sites_FullAccess"
$Sites_FullAccess_Secret = "~c754~9vkHcvUajPsDr.eH1X59iHEJ4d3FjybcYy"
$Tenant_Id = "601141a3-a812-4670-ac30-03f9db120f15"
$Tenant_DN = "myorg.onmicrosoft.com"
$CertThumbprint = "7CAE02F3A045258758522FF54552D356A6753159"
$securePasswordString = ConvertTo-SecureString "mY_Certif1cate-PassCDRD" -AsPlainText -Force
$CertificatePath = "C:\temp\Sites_FullAccess Certificate.pfx"
$SiteUrl = "https://myorg.sharepoint.com/sites/mysite"

Connect-PnPOnline -Url $SiteUrl `
    -ClientId $Sites_FullAccess_App_ID `
    -CertificatePath $CertificatePath `
    -CertificatePassword  $securePasswordString `
    -Tenant $Tenant_DN `
    -Verbose `
    #-ReturnConnection

Grant-PnPAzureADAppSitePermission -Site $SiteUrl `
    -AppId $Sites_FullAccess_App_ID `
    -DisplayName $Sites_FullAccess_Name `
    -Permissions FullControl 
    
Add-PnPFile -Path "C:\temp\testfile.txt" -Folder "QuercusFiles" -Connection $connection


User's image

Microsoft 365 and Office | SharePoint | For business | Windows
{count} votes

3 answers

Sort by: Most helpful
  1. Xyza Xue_MSFT 30,241 Reputation points Microsoft External Staff
    2024-12-02T06:53:33.7966667+00:00

    Hi @na ,

    I would have the same problem as you by running the code you posted and am currently unable to run PnP Powershell using this login method.

    Here are steps:

    1.Register a new Azure AD Application ID and Grant Access to the tenant:

    Register-PnPEntraIDAppForInteractiveLogin -ApplicationName "PnP PowerShell" -SharePointDelegatePermissions "AllSites.FullControl" -Tenant Salaudeen.onmicrosoft.com -Interactive  
    
    

    2.Using the following PnP PowerShell to Upload File to SharePoint Online Document Library:

    #Config Variables
    $SiteURL = "https://yourdomain.sharepoint.com/sites/AmyTeam"
    $ClientID = "4fad20a5-9008-47d2-9560-f7ff0ddf8c94"
    $SourceFilePath ="C:\SharePoint dev Readiness.docx"
    $DestinationPath = "Shared Documents" #Site Relative Path of the Library
     
      
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive -ClientId $ClientID
          
    #powershell pnp to upload file to sharepoint online
    Add-PnPFile -Path $SourceFilePath -Folder $DestinationPath
    
    

    Output:

    User's image


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. na 146 Reputation points
    2024-12-03T14:07:42.8133333+00:00

    Thanks for the comments.
    You are correct, the $connection variable is not defined in what i posted. Apologies for this.
    I was trying to pass the connection as a variable, which is allowed with PNP.

    The problem was eventually solved by forcing the install of the latest pre-release of pnpOnline:

    Install-Module PnP.PowerShell -AllowPrerelease -SkipPublisherCheck -Force
    
    
    
    

    I assume what i was experiencing was a recent bug, as i had only just installed the latest release version of PnP online. Not ideal using a prerelease version, but its working now at least.

    0 comments No comments

  3. Xyza Xue_MSFT 30,241 Reputation points Microsoft External Staff
    2024-12-04T01:35:17.6566667+00:00

    Hi @na ,

    I'm glad to hear you solve the problem, I would make a brief summary of this thread:

    Issue Symptom:

    Unable to upload files to sharepoint using PnP.Powershell - constantly getting 401 error.

    Current status:

    The problem was eventually solved by forcing the install of the latest pre-release of pnpOnline:

    Install-Module PnP.PowerShell -AllowPrerelease -SkipPublisherCheck -Force
    

    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community member's to see the useful information when reading this thread. Thanks for your understanding!


    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.