/
How to generate an access token using PowerShell
How to generate an access token using PowerShell
Gord Russell wrote auth.ps1 (thanks!) to generate an access token using Microsoft PowerShell. It implements the Invoke-RestMethod to send an HTTPS POST request to the VSS RESTful web service, then PowerShell deserializes the JavaScript Object Notation (JSON) content into objects.
By calling the script as $TK = .\auth.ps1, $TK is populated with a PS object containing the token.
Step-by-step guide
The following steps explain how auth.ps1 works:
Request username and password:
$user = Read-Host -Prompt 'User name' $pass = Read-Host -Prompt 'password' -AsSecureString
Convert secureString to plain text:
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass) $UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
Generate request Headers with Authentication: Basic:
$pair = $user + ":" + $UnsecurePassword # Encode the string to the RFC2045-MIME variant of Base64, except not limited to 76 char/line. $bytes = [System.Text.Encoding]::ASCII.GetBytes($pair) $base64 = [System.Convert]::ToBase64String($bytes) # Create the Auth value as the method, a space, and then the encoded pair Method Base64String $basicAuthValue = "Basic $base64"
Make the POST request to /auth/request-token
Invoke-RestMethod -uri "https://vss-api.eis.utoronto.ca/auth/request-token" -Headers $headers -method POST
After populating $TK, it can be used for any of the API calls by:
$authVal = "Basic " + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($tk.token)) Invoke-RestMethod -uri "https://vss-ws.eis.utoronto.ca/v2/vm" -Headers @{"AUTHORIZATION"=$authVal}
Related articles
, multiple selections available,
Related content
Multi Factor Authentication with Time-based One-Time Password (TOTP) with the vss-cli
Multi Factor Authentication with Time-based One-Time Password (TOTP) with the vss-cli
More like this
VSS: using TOTP in vss cli scripting
VSS: using TOTP in vss cli scripting
More like this
Multi Factor Authentication with Time-based One-Time Password (TOTP)
Multi Factor Authentication with Time-based One-Time Password (TOTP)
More like this
How-to enable Multi-Factor Authentication (MFA) on the ITS Private Cloud VPN (VSS VPN)
How-to enable Multi-Factor Authentication (MFA) on the ITS Private Cloud VPN (VSS VPN)
More like this
Configure the vss-cli
Configure the vss-cli
More like this
Multi-Factor Authentication with TOTP
Multi-Factor Authentication with TOTP
More like this