I have written this code
$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()
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":"Access denied. You do not have permission to perform this action or access this resource."}}} _api/web/lists/getbytitle('test')/rootfolder/files/add(url='BaselineFinishTag_2014_06.log', overwrite=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.
Can you tell me what I have missed in this code which is causing me to get UnAuthorizedException?