-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGet-AccessToken.ps1
More file actions
33 lines (30 loc) · 969 Bytes
/
Get-AccessToken.ps1
File metadata and controls
33 lines (30 loc) · 969 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
function Get-AccessToken {
param(
[Parameter(Mandatory = $true)]
[string]$tenantId,
[Parameter(Mandatory = $true)]
[string]$clientId,
[Parameter(Mandatory = $true)]
[string]$clientSecret
)
$global:token = ""
$graphResource = 'https://graph.microsoft.com/'
$oAuthUri = "https://login.windows.net/$TenantId/oauth2/token"
$authBody = [Ordered]@{
resource = $graphResource
client_id = $clientId
client_secret = $clientSecret
grant_type = 'client_credentials'
}
Write-Host "[A] Authenticating to tenant's $TenantId Graph API"
try {
$authResponse = Invoke-RestMethod -Method Post -Uri $oAuthUri -Body $authBody -ErrorAction Stop
$token = $authResponse.access_token
} catch {
Write-Host -ForegroundColor Red "[!] Authentication failed: $_"
exit 1
}
Write-Host "[*] Authentication successful"
Write-Host "[*] Access token obtained successfully"
return $token
}