Public/Get-BBAccessToken.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
Function Get-BBAccessToken { <# .SYNOPSIS Get access token to access GoBright API .DESCRIPTION Get access token to access GoBright API .PARAMETER BrightBookingApiUrl Address of the GoBright API, e.g.: https://t1b.gobright.cloud/ .PARAMETER BrightBookingApiKey API key to access the api .EXAMPLE Get-BBAccessToken -BrightBookingApiUrl "https://t1b.gobright.cloud/" -BrightBookingApiKey "[your api key]" .LINK https://support.gobright.com/ #> [CmdletBinding()] Param( [Parameter(Mandatory=$True)] [string]$BrightBookingApiUrl, [Parameter(Mandatory=$True)] [string]$BrightBookingApiKey ) Process { $url = $BrightBookingApiUrl $resturi = [System.Uri]::new([System.Uri]::new($url), "/token") $body = @{ grant_type = "apikey" apikey = $BrightBookingApiKey } $hdrs = @{} Try { $response = Invoke-WebRequest -Uri $resturi -Method Post -Body $body -ContentType 'application/text' -Headers $hdrs -UseBasicParsing If ($response.StatusCode -eq 200) { $jsonresponse = $response.Content | ConvertFrom-Json $access_token = $jsonresponse.access_token return $access_token } } Catch { $statusCode = $_.Exception.Response.StatusCode.Value__ $responseText = $_ Try { $jsonresponse = $responseText | ConvertFrom-Json If ($jsonresponse.SyncRoot) { $statusMessage = $jsonresponse.SyncRoot } Else { $statusMessage = $responseText } } Catch { $statusMessage = $responseText } throw "Failed to get access token from the API, check your API key. (statuscode: $statusCode, message: $statusMessage)" } } } |