Generate your HCP Anywhere Auth Token with PowerShell using API

Document created by Carlos Vargas on Apr 22, 2016Last modified by Michael Ratner on Sep 18, 2017
Version 4Show Document
  • View in full screen mode

A great new feature in the latest version of HCP Anywhere is how to programmatically access the HCP Anywhere folders from your account. In this quick post I will describe how to do this via PowerShell.

 

1. We need to a couple of functions. One to generate a Basic Authentication Token and to convert the password .

HCPAWFSSAPI1.png

 

2. Let's collect the information of the HCP Anywhere Pod that we want to connect and the username and password for authentication

HCPAWFSSAPI2.png

 

3. Let's build the headers and body for our request

HCPAWFSSAPI3.png

 

4. Now we need to put all the pieces together so we can get our token.

HCPAWFSSAPI4.png

 

5. Now execute your script

HCPAWFSSAPI5.png

 

 

Complete Script

 

###########################################################################################

#                                                                                         #

#  Name:  Get-HCPAWAPIToken.ps1                                                           #

#  Author: Carlos Vargas                                                                  #

#  Purpose: This script will help you request your new HCP Anywhere FSS API Token         #

#           for your application                                                          #

###########################################################################################

 

##############

# Functions

##############

 

Function GetBasicAuthHeader($value){

 

$valuebase64 = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($value))

 

$BasicAuthHeader = $valuebase64

$BasicAuthHeader

}

 

function ConvertFrom-SecureToPlain {

  

    param( [Parameter(Mandatory=$true)][System.Security.SecureString] $SecurePassword)

  

    # Create a "password pointer"

    $PasswordPointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)

  

    # Get the plain text version of the password

    $PlainTextPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto($PasswordPointer)

  

    # Free the pointer

    [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($PasswordPointer)

  

    # Return the plain text password

    $PlainTextPassword

  

}

 

###########

# Intro

###########

cls

Write-Host "******************* Welcome to the HCP Anywhere FSS API Token Generator Script **********************" -ForegroundColor Yellow

 

# Get HCPAW System, User and Password

$HCPAWURL = Read-Host "Please Type the HCP Anywhere System you wish to connect too. Ex. HCPAnywhere.domain.dom"

$HCPAWUser = Read-Host "Type your HCP Anywhere User Name. Ex. jsmith"

$HCPAWPassEnc = Read-Host "Type your HCP Anywhere User password." -AsSecureString

$HCPAWPassPlain = ConvertFrom-SecureToPlain($HCPAWPassEnc)

$HCPAWAPIURL = "https://$HCPAWURL/fss/public/login/oauth"

 

# Set all the headers for your connection

$headers = @{}

$headers["X-HCPAW-FSS-API-VERSION"] = "2.1.1"

$headers["Accept"] = "application/json"

$BasicAuthValue = GetBasicAuthHeader "$($HCPAWUser):$($HCPAWPassPlain)"

$BasicAuth = "Basic $BasicAuthValue"

$headers["Authorization"] = $BasicAuth

 

# Set the request that will go in the body of the Rest API request

$body = @{}

$body["grant_type"] = "urn:hds:oauth:negotiate-client"

 

# Save Response from HCP Anywhere

$TokenResponse = Invoke-RestMethod -Headers $headers -ContentType "application/x-www-form-urlencoded"  -Uri $HCPAWAPIURL -Method Post -Body $body #-Verbose

 

$HCPAWaccesstoken = $TokenResponse.access_token

$HCPAWTokenType = $TokenResponse.token_type

$HCPAWTokenExpiration = $TokenResponse.expires_in

 

Write-Host "Your HCPAW Token is:  $HCPAWaccesstoken"

Write-Host "Your HCPAW Token Type is: $HCPAWTokenType"

Write-Host "Your HCPAW Token will expire in $HCPAWTokenExpiration seconds"

Attachments

    Outcomes