forked from kine/NVRAppDevOps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-OAuth2.ps1
41 lines (38 loc) · 1.28 KB
/
Get-OAuth2.ps1
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
function Get-OAuth2
{
<#
.SYNOPSIS
Gets bearer access token
.DESCRIPTION
Uses Office 365 Application ID and Application Secret to generate the token
.PARAMETER AppId
Microsoft Azure Application ID.
.PARAMETER AppSecret
Microsoft Azure Application secret.
.PARAMETER Credentials
Username and password of the user to authenticate
#>
Param (
[parameter(Mandatory = $true)]
[string]$AppId,
[parameter(Mandatory = $true)]
[string]$AppSecret,
[parameter(Mandatory = $true)]
[pscredential]$Credentials,
[parameter(Mandatory = $true)]
[string]$Tenant,
[string]$Resource='https://api.businesscentral.dynamics.com'
)
$Uri = "https://login.microsoftonline.com/$Tenant/oauth2/token"
$Body = @{
resource = $Resource
grant_type = 'password'
username = $Credentials.UserName
password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credentials.Password))
client_id = $AppId
client_secret = $AppSecret
}
$AuthResult = Invoke-RestMethod -Method Post -Uri $Uri -Body $Body
#Function output
return $AuthResult
}