-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-oAuthToken.ps1
More file actions
47 lines (45 loc) · 2.16 KB
/
Copy pathGet-oAuthToken.ps1
File metadata and controls
47 lines (45 loc) · 2.16 KB
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
38
39
40
41
42
43
44
45
46
47
function Get-oAuthToken {
<#
.SYNOPSIS
Function to connect to the Microsoft login OAuth endpoint and return an OAuth token.
.DESCRIPTION
This Function connects to the Microsoft AAD OAuth endpoint and generates an OAuth token.
This token can then be used for authentication against the resource supplied In the parameters.
.PARAMETER ClientID
The ClientID of the application used for authentication against Azure AD.
.PARAMETER ClientSecret
The Key generated within the application used for authentication against Azure AD.
This key should have rights to the resource supplied in the ResourceName parameter.
.PARAMETER TenantId
The TenantId of the Azure AD that you wish to authenticate against.
.PARAMETER ResourceName
The name of the resource that you want to generate a token for.
.EXAMPLE
Get-ApiToken -ClientID '12345678-9012-3456-7890-123456789012' -ClientSecret 'abcdefghijklmnopqrstuvwxyz==' -TenantId 'abcd4ffb-d0bc-1234-854a-114710c94dbb' -Resource 'https://test.onmicrosoft.com/apitest'
.NOTES
Version 2.0
#>
[Cmdletbinding()]
Param(
[Parameter(Mandatory = $true)][string]$ClientID,
[Parameter(Mandatory = $true)][string]$ClientSecret,
[Parameter(Mandatory = $true)][string]$TenantId,
[Parameter(Mandatory = $false)][string]$ResourceName = 'https://graph.microsoft.com/.default',
[Parameter(Mandatory = $false)][boolean]$ChinaAuth = $false,
[Parameter(Mandatory = $false)][boolean]$IncludeType = $true
)
#This script will require the Web Application and permissions configured in Azure Active Directory.
if ($ChinaAuth) {
$LoginURL = 'https://login.chinacloudapi.cn'
$ResourceName = $ResourceName.replace('microsoft.com', 'chinacloudapi.cn')
} else {
$LoginURL = 'https://login.microsoftonline.com'
}
#Get an Oauth 2 access token based on client id, secret and tenant id
$Body = @{grant_type = 'client_credentials'; scope = $ResourceName; client_id = $ClientID; client_secret = $ClientSecret }
$AuthContext = Invoke-RestMethod -Method Post -Uri $LoginURL/$TenantId/oauth2/v2.0/token -Body $Body
if ($IncludeType) {
Return ('{0} {1}' -f $AuthContext.token_type, $AuthContext.access_token)
}
Return $AuthContext.access_token
}