Examples/Authentication.ps1
#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 |