-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathGetToken-MSALDeviceCodeAuth.ps1
More file actions
37 lines (24 loc) · 956 Bytes
/
Copy pathGetToken-MSALDeviceCodeAuth.ps1
File metadata and controls
37 lines (24 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#Requires -Modules "MSAL.PS"
cls
$currentPath = (Split-Path $MyInvocation.MyCommand.Definition -Parent)
$clientId = ""
$authority = "https://login.microsoftonline.com/common"
$apiResource = "https://analysis.windows.net/powerbi/api"
$scopes = @("$apiResource/.default")
$app = New-MsalClientApplication -ClientId $clientId -Authority $authority
# Warning - Dont work with PowerShell 7, must use powershell5
Enable-MsalTokenCacheOnDisk $app
Write-Host "Cache file: $([TokenCacheHelper]::CacheFilePath)"
try {
$token = $app | Get-MsalToken -Scopes $scopes -Silent
}
catch {
Write-Host "Getting token"
$token = $app | Get-MsalToken -Scopes $scopes -DeviceCode -Silent
}
$headers = @{
'Content-Type'= "application/json"
'Authorization'= "Bearer $($token.AccessToken)"
}
$response = Invoke-RestMethod -Uri "https://api.powerbi.com/v1.0/myorg/admin/apps?`$top=100" -Headers $headers -Method Get -ContentType $contentType
$response.value