Skip to content

Instantly share code, notes, and snippets.

@jessewolcott
Created May 7, 2023 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessewolcott/2c72f575f544180cfd83df13555a50f9 to your computer and use it in GitHub Desktop.
Save jessewolcott/2c72f575f544180cfd83df13555a50f9 to your computer and use it in GitHub Desktop.
$AppId = ""
$Client_Secret = ""
$TokenURL = "" # Example: https://www.example.com/oauth2/token
$Hostname = "" # Example: https://www.example.com/
$RequestBasePath = "" # Example: api/config/v1
$APIPath = "" # Example: /users
$Body = @{
client_id = $AppId
client_secret = $Client_Secret
grant_type = "client_credentials"
}
try {
Write-Output "Getting API Auth Token"
$TokenRequest = Invoke-WebRequest -Method Post -Uri $TokenURL -ContentType "application/x-www-form-urlencoded" -Body $Body -UseBasicParsing -ErrorAction Stop }
catch { Write-Output "Unable to obtain access token, moving on..."}
if ($TokenRequest){
$Token = ($tokenRequest.Content | ConvertFrom-Json).access_token
$AuthHeader1 = @{
'Content-Type'='application\json'
'Authorization'="Bearer $Token"
}
$URL=($Hostname+$RequestBasePath+$APIPath)
$RequestContent = (Invoke-WebRequest -Headers $AuthHeader1 -Uri $URL -Method GET).Content | ConvertFrom-Json
}
if ($RequestContent){
$RequestContent | Out-gridview
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment