Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Current »

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:

  1. Request username and password:

    $user = Read-Host -Prompt 'User name'
    $pass = Read-Host -Prompt 'password' -AsSecureString
  2. Convert secureString to plain text:

    $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass)
    $UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
  3. 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"
  4. Make the POST request to /auth/request-token

    Invoke-RestMethod -uri "https://vss-api.eis.utoronto.ca/auth/request-token" -Headers $headers -method POST
  5. 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}

  • No labels