Skip to content

Instantly share code, notes, and snippets.

@vgrem
Last active May 21, 2019 18:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrem/fe0cda40b5ed0cd9070c to your computer and use it in GitHub Desktop.
Save vgrem/fe0cda40b5ed0cd9070c to your computer and use it in GitHub Desktop.
.\Get-SPOAccessToken.ps1
<#
.Synopsis
Sends an HTTP or HTTPS request to a SharePoint Online REST-compliant web service.
.DESCRIPTION
This function sends an HTTP or HTTPS request to a Representational State
Transfer (REST)-compliant ("RESTful") SharePoint Online web service.
.EXAMPLE
Invoke-SPORestMethod -Uri "https://contoso.sharepoint.com/_api/web" -AccessToken $accessToken
#>
Function Invoke-SPORestMethod()
{
Param(
[Uri]$Uri,
[Object]$Body,
[Hashtable]$Headers,
[Microsoft.PowerShell.Commands.WebRequestMethod]$Method = [Microsoft.PowerShell.Commands.WebRequestMethod]::Get,
[string]$AccessToken
)
$contentType = 'application/json;odata=verbose'
if(-not $Headers) {
$Headers = @{}
}
$Headers["Accept"] = "application/json;odata=verbose"
$Headers.Add('Authorization','Bearer ' + $AccessToken)
Invoke-RestMethod -Method $Method -Uri $Uri -ContentType $contentType -Headers $Headers -Body $Body
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment