i am receiving the error message below when trying to download documents from SharePoint online. i am able to connect and retrieve the properties of the items in the document library. But when i then try to download the files i get a 403 forbidden error.
anybody able to point me in the right direction or spot what i could be doing wrong
Code
[Reflection.Assembly]::LoadFile("C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\ClaimsAuth.dll")
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$spUrl = "https://mysharepointsite/sites/COP/SharePoint/Buko"
$listTitle = "Documents"
$outPath = "C:\Users\BUKO\Downloads\CRAP\Powershell downloads"
$spoContext = New-Object SPOContext("$spUrl")
$web = $spoContext.Web;
$List = $web.Lists.GetByTitle($listTitle)
#$list = $spoContext.Web.Lists.GetByTitle($listTitle)
$qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$items = $list.GetItems($qry)
$spoContext.Load($list)
$spoContext.Load($items)
$spoContext.ExecuteQuery()
$siteUri = [Uri]$spUrl
$client = new-object System.Net.WebClient
$client.UseDefaultCredentials=$true
$client
$siteUri
if ( -not (Test-Path $outPath) ) {
New-Item $outPath -Type Directory | Out-Null
}
$items | % {
$url = new-object Uri($siteUri, $_["FileRef"])
$fileName = $_["FileLeafRef"]
$outFile = Join-Path $outPath $fileName
Write-Host "Downloading $url to $outFile"
$client.DownloadFile( $url, $outFile )
}ERROR MESSAGE
Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (403) Forbidden." At C:\Users\BUKO\Documents\Powershell Projects Scripts\tests2.ps1:36 char:25+ $client.DownloadFile( $url, $outFile )+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException+ FullyQualifiedErrorId : WebException