52 lines
2.0 KiB
PowerShell
52 lines
2.0 KiB
PowerShell
<#
|
|
Script to generate bearer token for Egnyte API. Bearer token is then used in future reqeusts.
|
|
#>
|
|
|
|
$BasePath = 'https://mpe.egnyte.com'
|
|
$OAuthPath = $BasePath + '/puboauth/token'
|
|
|
|
# Get path to store bearer.token file.
|
|
if (!($Tokenfolderpath)) {
|
|
Add-Type -AssemblyName System.Windows.Forms
|
|
$FileBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
|
|
$FileBrowser.Description = 'Locate bearer.token file'
|
|
$FileBrowser.UseDescriptionForTitle
|
|
$null = $FileBrowser.ShowDialog()
|
|
$Tokenfolderpath = $FileBrowser.SelectedPath
|
|
|
|
}
|
|
|
|
$key_path = $Tokenfolderpath + '\Egnyte Key.token'
|
|
$Secret_path = $Tokenfolderpath + '\Egnyte Secret.token'
|
|
|
|
|
|
$client_id = get-content -Path $key_path | ConvertTo-SecureString | ConvertFrom-SecureString -AsPlainText
|
|
$client_secret = get-content -Path $Secret_path | ConvertTo-SecureString | ConvertFrom-SecureString -AsPlainText
|
|
if (!($EG_User)) {
|
|
$EG_User = read-host -Prompt "Enter your Egnyte (non-sso) username. The API will impersonate this user:"
|
|
}
|
|
|
|
if (!($EG_Password)) {
|
|
$EG_Password = read-host -Prompt "Enter your Egnyte (non-sso) Password" -MaskInput
|
|
}
|
|
|
|
|
|
$eg_grant_type = 'grant_type=password'
|
|
$EG_Scopes = 'Egnyte.permission Egnyte.filesystem'
|
|
|
|
$eg_auth_token = "client_id=$client_id&client_secret=$client_secret&username=$EG_User&password=$EG_Password&$eg_grant_type&scope=$eg_scopes" #| ConvertTo-SecureString -AsPlainText
|
|
|
|
$eg_header = @{
|
|
'Content-Type' = 'application/x-www-form-urlencoded'
|
|
'Connection' = 'close'
|
|
'grant_type' = 'password'
|
|
}
|
|
|
|
# Send actual request
|
|
$eg_auth_response = Invoke-WebRequest -Uri $OAuthPath -HttpVersion 1.1 -Method Post -body $eg_auth_token -Headers $eg_header
|
|
|
|
$eg_bearer_token = ($eg_auth_response.Content | ConvertFrom-Json).access_token
|
|
|
|
#Save Bearer Token to file for furture use
|
|
$TokenPath = $Tokenfolderpath + '\' + $env:COMPUTERNAME + '-' + $env:USERNAME + ' Bearer.token'
|
|
$eg_bearer_token | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Set-Content -Path $TokenPath |