我写了这段代码
$spSiteUrl = "http://mysharepoint/sites/site/web/"
$cmd = "_api/web/lists/getbytitle('$docLib')/rootfolder/files/add(url='" + $file.Name + "', overwrite=true)"
$digest = "got valid digest through code";
$mediaType = new-object("System.Net.Http.Headers.MediaTypeWithQualityHeaderValue") "application/json"
$handler = new-object("System.Net.Http.HttpClientHandler")
$handler.UseDefaultCredentials= $true
$client = New-Object("System.Net.Http.HttpClient") $handler
$client.BaseAddress = $spSiteUrl
$client.DefaultRequestHeaders.Accept.Clear()
$client.DefaultRequestHeaders.Accept.Add($mediaType);
$client.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose")
$content = $null
$client.DefaultRequestHeaders.Add("X-HTTP-Method", "PUT")
$client.DefaultRequestHeaders.Add("X-RequestDigest", $digest)
$fileStream = [System.IO.File]::OpenRead($file.FullName)
$streamContent = new-object ("System.Net.Http.StreamContent") $fileStream
$task = $client.PostAsync($cmd, $streamContent)
$response = $task.Result
$content = $response.Content.ReadAsStringAsync().Result
Write-Host $content
$fileStream.Close()
$fileStream.Dispose()
$response = $response.EnsureSuccessStatusCode()
$client.Dispose()
这里我已经有了一个有效的摘要值,我从POST到_api / contextinfo获得了
但是当我执行此代码时出现错误
{"错误":{"代码":" -2147024891,System.UnauthorizedAccessException","消息":{ " lang":" en-US"," value":"访问被拒绝。您无权执行此操作或访问此资源。"}}} _api / web / lists / getbytitle(' test')/ rootfolder / files / add(url =' BaselineFinishTag_2014_06.log',overwrite = true)
在这里您可以看到我使用UseDefaultCredentials为true。此代码与一个帐户一起运行,该帐户是服务器场管理员和网站集管理员,并且拥有运行此代码的网站的完整所有权。
你能告诉我在这段代码中我错过了什么导致我得到UnAuthorizedException吗?
答案 0 :(得分:0)
Consuming the SharePoint 2013 REST API from PowerShell文章介绍了如何在PowerShell中使用REST API执行CRUD操作。
以下函数说明了如何使用指定文章中的Invoke-RestSPO.ps1函数通过SharePoint 2013 REST上传文件:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
. ".\Invoke-RestSPO.ps1" #Load Invoke-RestSPO function
Function Upload-SPOFile(){
Param(
[Parameter(Mandatory=$True)]
[String]$WebUrl,
[Parameter(Mandatory=$True)]
[String]$UserName,
[Parameter(Mandatory=$True)]
[String]$Password,
[Parameter(Mandatory=$True)]
[String]$FolderUrl,
[Parameter(Mandatory=$True)]
[System.IO.FileInfo]$FileInfo
)
$Url = $WebUrl + "/_api/web/GetFolderByServerRelativeUrl('" + $FolderUrl + "')/Files/add(url='" + $FileInfo.Name + "',overwrite=true)"
$FileContent = [System.IO.File]::ReadAllBytes($FileInfo.FullName)
$contextInfo = Get-SPOContextInfo $WebUrl $UserName $Password
Invoke-RestSPO -Url $Url -Method Post -UserName $UserName -Password $Password -Body $FileContent -RequestDigest $contextInfo.GetContextWebInformation.FormDigestValue
}
#Usage: upload file into SharePoint Online
$UserName = "username@contoso.onmicrosoft.com"
$Password = Read-Host -Prompt "Enter the password"
$WebUrl = "https://contoso.sharepoint.com/"
$FolderUrl = "/Shared Documents"
$UploadFileInfo = New-Object System.IO.FileInfo("C:\Users\user\Documents\SharePoint User Guide.docx")
Upload-SPOFile -WebUrl $WebUrl -UserName $UserName -Password $Password -FolderUrl $FolderUrl -FileInfo $UploadFileInfo