问题描述
我写了这段代码
$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 得到的
Here I already have a valid digest value which I got from doing a POST to _api/contextinfo
但是当我执行此代码时出现错误
But when I execute this code I get an error
{"error":{"code":"-2147024891, System.UnauthorizedAccessException","message":{"lang":"en-US","value":"拒绝访问.你这样做无权执行此操作或访问此资源."}}}_api/web/lists/getbytitle('test')/rootfolder/files/add(url='BaselineFinishTag_2014_06.log', overwrite=true)
在这里,您可以看到我使用 UseDefaultCredentials 为 true.此代码使用一个帐户运行,该帐户是场管理员和网站集管理员,并且拥有运行此代码的网站的完全所有权.
Here as you can see that I am using UseDefaultCredentials to true. This code is running with an account which is the farm admin and site collection administrator and has complete ownership of the site where this code is being run.
你能告诉我我在这段代码中遗漏了什么导致我得到 UnAuthorizedException 吗?
Can you tell me what I have missed in this code which is causing me to get UnAuthorizedException?
推荐答案
从 PowerShell 使用 SharePoint 2013 REST API 文章介绍了如何在 PowerShell 中使用 REST API 执行 CRUD 操作.
Consuming the SharePoint 2013 REST API from PowerShell article describes how to perform CRUD operations using REST API in PowerShell.
以下函数演示了如何使用 Invoke-RestSPO.ps1 通过 SharePoint 2013 REST 上传文件来自指定文章的函数:
The following function demonsrtates how to upload files via SharePoint 2013 REST using Invoke-RestSPO.ps1 function from the specified article:
[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 = "[email protected]"
$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
参考文献
- 调用-RestSPO.ps1
- 使用 SharePoint来自 PowerShell 的 2013 REST API
这篇关于使用 REST API 将文件上传到 SharePoint 2013的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!