Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrik23/33a7801fbc9b032a74f4bcc1a76b5ae4 to your computer and use it in GitHub Desktop.
Save mrik23/33a7801fbc9b032a74f4bcc1a76b5ae4 to your computer and use it in GitHub Desktop.
PowerShell script to demonstrate how to make calls to SharePoint Online API using authorization token retrieved with PnP PowerShell
<#
PowerShell script to demonstrate how to make calls to SharePoint Online API using authorization token retrieved with PnP PowerShell
This should work if the SharePoint tenant has LegacyAuthProtocolsEnabled as FALSE (legacy form based auth not authorized)
You need to have SharePoint Online PnP PowerShell installed https://github.com/SharePoint/PnP-PowerShell
#>
$spoUrl = "https://<tenant>.sharepoint.com"
$connection = Connect-PnPOnline -Url $spoUrl -Credentials UserCredential -ReturnConnection
$AuthAccessToken = Get-PnPAppAuthAccessToken -Connection $connection
$headers = @{
"Accept" = "application/json;odata=nometadata"
"Authorization" = "Bearer $AuthAccessToken"
}
$apiEndpoint = "/_api/Web"
$apiUri = $spUrl + $apiEndpoint
$res = Invoke-RestMethod -Method Get -Uri $apiUri -Headers $headers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment