Another option I managed to get working was to first retrieve a security token from https://login.microsoftonline.com/extSTS.srf, and then an access token from https://COMPANY.sharepoint.com/_forms/default.aspx?wa=wsignin1.0:
$Global:Credential = Get-Credential -Message "cred" -UserName "[email protected]"
function Get-StringBetweenTwoStrings($firstString, $secondString, $inputString){
#Regex pattern to compare two strings
$pattern = "$firstString(.*?)$secondString"
#Perform the opperation
$result = [regex]::Match($inputString,$pattern).Groups[1].Value
#Return result
return $result
}
function Get-AuthCookies {
$body =
@"
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"
xmlns:a="http://www.w3.org/2005/08/addressing"
xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<s:Header>
<a:Action s:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</a:Action>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<a:To s:mustUnderstand="1">https://login.microsoftonline.com/extSTS.srf</a:To>
<o:Security s:mustUnderstand="1"
xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<o:UsernameToken>
<o:Username>$($Global:Credential.UserName)</o:Username>
<o:Password>MyPassword</o:Password>
</o:UsernameToken>
</o:Security>
</s:Header>
<s:Body>
<t:RequestSecurityToken xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust">
<wsp:AppliesTo xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
<a:EndpointReference>
<a:Address>https://COMPANY.sharepoint.com/sites/ADV-DEV/assigDev/bassie/_api</a:Address>
</a:EndpointReference>
</wsp:AppliesTo>
<t:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</t:KeyType>
<t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType>
<t:TokenType>urn:oasis:names:tc:SAML:1.0:assertion</t:TokenType>
</t:RequestSecurityToken>
</s:Body>
</s:Envelope>
"@
$securityTokenUrl = "https://login.microsoftonline.com/extSTS.srf"
$accessTokenUrl = "https://COMPANY.sharepoint.com/_forms/default.aspx?wa=wsignin1.0"
$secTokenResponse = Invoke-RestMethod -Body $body -Uri $securityTokenUrl -Method POST
$token = $secTokenResponse.Envelope.Body.RequestSecurityTokenResponse.RequestedSecurityToken.BinarySecurityToken.InnerText
$accTokenResponse = Invoke-WebRequest -Body $token -Uri $accessTokenUrl -Method POST
$cookieHeaders = $accTokenResponse.Headers.'Set-Cookie'
return $cookieHeaders
}
We then want to use these tokens to construct some cookies which we will send with the web request:
$cookie = Get-AuthCookies
$rtFa = Get-StringBetweenTwoStrings -firstString "rtFa=" -secondString ";" -inputString $cookie
$FedAuth = Get-StringBetweenTwoStrings -firstString "FedAuth=" -secondString ";" -inputString $cookie
$url = "https://COMPANY.sharepoint.com/sites/ADV-DEV/assigDev/bassie/_api/lists/getByTitle('Test')/items"
[System.Uri]$Uri = $url
$ContentType = "application/json"
$Method = 'GET'
$Cookie1 = New-Object System.Net.Cookie
$Cookie1.Name = "rtFa" # Add the name of the cookie
$Cookie1.Value = $rtFa
$Cookie1.Domain = $uri.DnsSafeHost
$Cookie2 = New-Object System.Net.Cookie
$Cookie2.Name = "FedAuth" # Add the name of the cookie
$Cookie2.Value = $FedAuth
$Cookie2.Domain = $uri.DnsSafeHost
$WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$WebSession.Cookies.Add($Cookie1)
$WebSession.Cookies.Add($Cookie2)
Finally, send the web request.
# Splat the parameters
$props = @{
Uri = $uri.AbsoluteUri
Credential = $Credential
ContentType = $ContentType
Method = $Method
WebSession = $WebSession
}
$response = Invoke-RestMethod @props
$response.content.properties.Title
While this does work, it is pretty long-winded and unnecessarily complicated.
I ended up just using the Microsoft.SharePoint.Client library in C# to do what I needed - as all this needs is my credentials without the need to supply tokens..
I then realised I can use that same library in PowerShell with
$loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
$loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")