Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.