Skip to content

Commit 005d80b

Browse files
authored
feat: move function to MS graph (#14)
1 parent 0882c4d commit 005d80b

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

functions/Get-oAuthToken.ps1

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,43 @@ function Get-oAuthToken {
55
.DESCRIPTION
66
This Function connects to the Microsoft AAD OAuth endpoint and generates an OAuth token.
77
This token can then be used for authentication against the resource supplied In the parameters.
8-
.PARAMETER ApplicationId
9-
The ApplicationId of the application used for authentication against Azure AD.
10-
.PARAMETER ApplicationKey
8+
.PARAMETER ClientID
9+
The ClientID of the application used for authentication against Azure AD.
10+
.PARAMETER ClientSecret
1111
The Key generated within the application used for authentication against Azure AD.
1212
This key should have rights to the resource supplied in the ResourceName parameter.
1313
.PARAMETER TenantId
1414
The TenantId of the Azure AD that you wish to authenticate against.
1515
.PARAMETER ResourceName
1616
The name of the resource that you want to generate a token for.
1717
.EXAMPLE
18-
Get-ApiToken -ApplicationId '12345678-9012-3456-7890-123456789012' -ApplicationKey 'AfXooIr8rswX24yrFXMrO4SbBgutwTtojAZEpQOaaaa=' -TenantId 'abcd4ffb-d0bc-1234-854a-114710c94dbb' -Resource 'https://test.onmicrosoft.com/apitest'
18+
Get-ApiToken -ClientID '12345678-9012-3456-7890-123456789012' -ClientSecret 'abcdefghijklmnopqrstuvwxyz==' -TenantId 'abcd4ffb-d0bc-1234-854a-114710c94dbb' -Resource 'https://test.onmicrosoft.com/apitest'
1919
.NOTES
20-
Version 1.2.0
20+
Version 2.0
2121
#>
2222
[Cmdletbinding()]
2323
Param(
24-
[Parameter(Mandatory = $true)][string]$ApplicationId,
25-
[Parameter(Mandatory = $true)][string]$ApplicationKey,
24+
[Parameter(Mandatory = $true)][string]$ClientID,
25+
[Parameter(Mandatory = $true)][string]$ClientSecret,
2626
[Parameter(Mandatory = $true)][string]$TenantId,
27-
[Parameter(Mandatory = $false)][string]$ResourceName = "https://graph.windows.net",
28-
[Parameter(Mandatory = $false)][boolean]$ChinaAuth = $false
27+
[Parameter(Mandatory = $false)][string]$ResourceName = 'https://graph.microsoft.com/.default',
28+
[Parameter(Mandatory = $false)][boolean]$ChinaAuth = $false,
29+
[Parameter(Mandatory = $false)][boolean]$IncludeType = $true
2930
)
3031

3132
#This script will require the Web Application and permissions configured in Azure Active Directory.
3233
if ($ChinaAuth) {
3334
$LoginURL = 'https://login.chinacloudapi.cn'
34-
$ResourceName = $ResourceName.replace("windows.net", "chinacloudapi.cn")
35-
}
36-
else {
37-
$LoginURL = 'https://login.windows.net'
35+
$ResourceName = $ResourceName.replace('microsoft.com', 'chinacloudapi.cn')
36+
} else {
37+
$LoginURL = 'https://login.microsoftonline.com'
3838
}
3939

4040
#Get an Oauth 2 access token based on client id, secret and tenant id
41-
$Body = @{grant_type = "client_credentials"; resource = $ResourceName; client_id = $ApplicationId; client_secret = $ApplicationKey}
42-
$AuthContext = Invoke-RestMethod -Method Post -Uri $LoginURL/$TenantId/oauth2/token?api-version=1.0 -Body $Body
43-
Return "$($AuthContext.token_type) $($AuthContext.access_token)"
41+
$Body = @{grant_type = 'client_credentials'; scope = $ResourceName; client_id = $ClientID; client_secret = $ClientSecret }
42+
$AuthContext = Invoke-RestMethod -Method Post -Uri $LoginURL/$TenantId/oauth2/v2.0/token -Body $Body
43+
if ($IncludeType) {
44+
Return ('{0} {1}' -f $AuthContext.token_type, $AuthContext.access_token)
45+
}
46+
Return $AuthContext.access_token
4447
}

0 commit comments

Comments
 (0)