Last active
October 25, 2018 15:51
-
-
Save hthoma/8715fc28523270141aa11cb8c87d4138 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Generate-JWT ( | |
[Parameter(Mandatory = $True)] | |
[ValidateSet("HS256", "HS384", "HS512")] | |
$Algorithm = $null, | |
$type = $null, | |
[Parameter(Mandatory = $True)] | |
[string]$Issuer = $null, | |
[int]$ValidforSeconds = $null, | |
[Parameter(Mandatory = $True)] | |
$SecretKey = $null | |
){ | |
$exp = [int][double]::parse((Get-Date -Date $((Get-Date).addseconds($ValidforSeconds).ToUniversalTime()) -UFormat %s)) # Grab Unix Epoch Timestamp and add desired expiration. | |
[hashtable]$header = @{alg = $Algorithm; typ = $type} | |
[hashtable]$payload = @{iss = $Issuer; exp = $exp} | |
$headerjson = $header | ConvertTo-Json -Compress | |
$payloadjson = $payload | ConvertTo-Json -Compress | |
$headerjsonbase64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($headerjson)).Split('=')[0].Replace('+', '-').Replace('/', '_') | |
$payloadjsonbase64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($payloadjson)).Split('=')[0].Replace('+', '-').Replace('/', '_') | |
$ToBeSigned = $headerjsonbase64 + "." + $payloadjsonbase64 | |
$SigningAlgorithm = switch ($Algorithm) { | |
"HS256" {New-Object System.Security.Cryptography.HMACSHA256} | |
"HS384" {New-Object System.Security.Cryptography.HMACSHA384} | |
"HS512" {New-Object System.Security.Cryptography.HMACSHA512} | |
} | |
$SigningAlgorithm.Key = [System.Text.Encoding]::UTF8.GetBytes($SecretKey) | |
$Signature = [Convert]::ToBase64String($SigningAlgorithm.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($ToBeSigned))).Split('=')[0].Replace('+', '-').Replace('/', '_') | |
$token = "$headerjsonbase64.$payloadjsonbase64.$Signature" | |
$token | |
} | |
$api_key = '' | |
$api_secret = '' | |
Generate-JWT -Algorithm 'HS256' -type 'JWT' -Issuer $api_key -SecretKey $api_secret -ValidforSeconds 30 | |
# Generate JWT for use in API calls. | |
$token = Generate-JWT -Algorithm 'HS256' -type 'JWT' -Issuer $api_key -SecretKey $api_secret -ValidforSeconds 30 | |
# Generate Header for API calls. | |
[string]$contentType = 'application/json' | |
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" | |
$headers.Add('Content-Type' , $contentType) | |
$headers.Add('Authorization','Bearer ' + $token) | |
# Pull all API device data. | |
$query_result = Invoke-RestMethod -Uri "$base_uri/metrics/zoomrooms?page_size=300&page_number=$page_number" -Headers $headers -Method GET |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment