-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAuthentication.ps1
42 lines (37 loc) · 2.25 KB
/
Authentication.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
42
#Requires -Modules PartnerCenterCommunity
# Import-Module .\ -Force
# Import-Module ..\ -Force
# Create a web app (admin user -> web app) #
# Done once.
$WebApp = New-PartnerWebApp -DisplayName ('Test ' + (Get-Date -Format s)) -StayConnected
Write-Warning ('Remember to update the WebApp secret on {0}.' -f $WebApp.SecretExpiration)
# Get an authorization code (web app -> authorization code) #
# Get a refresh token (authorization code -> refresh token) #
# Need to be done once, or if the refresh token was not used for 90 days and expired.
Write-Warning 'Sleeping for 50 seconds to allow app creation on O365.'
Start-Sleep -Seconds 50
$RefreshToken = New-PartnerRefreshToken -Credential $WebApp.Credential
# Lets save our credentials so we can run the next part non-interactively #
# In this example to file, but preferably to something like a vault.
$ConnectionCredentials = @{
Credential = $WebApp.Credential
RefreshToken = $RefreshToken
Tenant = $WebApp.Tenant
}
$ConnectionCredentials | Export-Clixml -Path (Join-Path ([Environment]::GetFolderPath('Desktop')) SaveMeSecurely.xml)
# Get an access token (refresh token -> access token) #
# Connecting will do this automatically (it will also regenerate a new RefreshToken when needed automatically)
# Use `-RefreshTokenScript` to save the now generated extended "refresh token" so it will not need to be renewed in 90 days.
$ConnectionCredentials = Import-Clixml -Path (Join-Path ([Environment]::GetFolderPath('Desktop')) SaveMeSecurely.xml)
$null = Connect-PartnerCenter @ConnectionCredentials -RefreshTokenScript {
param ($AccessToken)
Write-Debug ("Saving updated RefreshToken: {0}***{1}" -f $AccessToken.RefreshToken.Substring(0, 4), $AccessToken.RefreshToken.Substring($AccessToken.RefreshToken.Length - 4, 4)) #-Debug
$ConnectionCredentials = Import-Clixml -Path (Join-Path ([Environment]::GetFolderPath('Desktop')) SaveMeSecurely.xml)
$ConnectionCredentials.RefreshToken = $AccessToken.RefreshToken
$ConnectionCredentials | Export-Clixml -Path (Join-Path ([Environment]::GetFolderPath('Desktop')) SaveMeSecurely.xml)
}
# Alternatively can be generated by:
$AccessToken = New-PartnerAccessToken @ConnectionCredentials
# Make a Partner Center API call #
# For example:
Get-PartnerOrganizationProfile