How To Get Bearer Token for CloudBolt API in PowerShell

Overview

The steps below will enable you to get a Bearer token using your credentials, which you can then use to make API calls using PowerShell.

1:

add-type @"
   using ;
   using System.Security.Cryptography.X509Certificates;
   public class TrustAllCertsPolicy : ICertificatePolicy {
   public bool CheckValidationResult(
   ServicePoint srvPoint, X509Certificate certificate,
   WebRequest request, int certificateProblem) {
   return true;
   }
   }
  "@

2:

TLS 1.0 is not supported on the CloudBolt server, the block below initiates a TLS 1.2 connection

[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[System.Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

3:

This is the request for the bearer token using your CloudBolt credentials

  $postparam=@{username='yourusername';password='yourpassword'} 
  $response=Invoke-WebRequest -Uri https://yourCloudBoltServer/api/v2/api-token-auth/ -Method Post -Body $postparam - 
    UseBasicParsing

4:

Grab the token value from the response of the above call

  $hash=$response.Content |ConvertFrom-Json
  $token= $hash.token

5:

Use the token to make your API call, example below to get a list of all Servers

    $response=Invoke-WebRequest -Uri https://yourCloudBoltServer/api/v2/api-token-auth/servers/ -Method Get -Headers @{'Authorization' = 'Bearer ' + $token} -UseBasicParsing 

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.