@@ -5,40 +5,43 @@ function Get-oAuthToken {
5
5
. DESCRIPTION
6
6
This Function connects to the Microsoft AAD OAuth endpoint and generates an OAuth token.
7
7
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
11
11
The Key generated within the application used for authentication against Azure AD.
12
12
This key should have rights to the resource supplied in the ResourceName parameter.
13
13
. PARAMETER TenantId
14
14
The TenantId of the Azure AD that you wish to authenticate against.
15
15
. PARAMETER ResourceName
16
16
The name of the resource that you want to generate a token for.
17
17
. 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'
19
19
. NOTES
20
- Version 1. 2.0
20
+ Version 2.0
21
21
#>
22
22
[Cmdletbinding ()]
23
23
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 ,
26
26
[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
29
30
)
30
31
31
32
# This script will require the Web Application and permissions configured in Azure Active Directory.
32
33
if ($ChinaAuth ) {
33
34
$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'
38
38
}
39
39
40
40
# 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
44
47
}
0 commit comments