Happy Holidays to All

Started by stanl, December 23, 2023, 07:14:04 AM

Previous topic - Next topic

stanl


nrr

And Happy Holidays to you as well.   
You've piqued my curiosity ... I'm now knee deep in learning REST and how to call it from PS.
Thanks again.
Nick

stanl

Quote from: nrr on December 25, 2023, 06:29:10 AM
And Happy Holidays to you as well.   
You've piqued my curiosity ... I'm now knee deep in learning REST and how to call it from PS.
Thanks again.
Nick


Appreciate your comment. I look at PS as it relates to WB as a quasi-extender... I was contacted by a co-worker manager after 5 years who needed help with oAuth tokens for a CRM with the goal of obtaining data into Power BI. The issue soon became coding to receive a new token from the refresh_token as the token expired in 24 hours.  As a template, this worked with PS

#This may be required for transfer protocol
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$shell = New-Object -ComObject wscript.shell
#import refresh token from csv file
$csv = Import-CSV -path "c:\temp\access token.csv"
$tokenEndpoint = "xxxxxxx"
$clientId = "xxxxx"
$clientSecret = "xxxxx"
$refreshtoken = $csv[0].refresh_token


$b = $shell.popup("Connecting to $tokenEndpoint",2,"Begin",4096)
#create body hash
$body = @{ 
'client_id'= $clientId
'client_secret' = $clientSecret
'grant_type' = 'refresh_token'
'refresh_token'= $refreshToken
}     
$headers=@{}
$headers.Add("Content-Type", "application/x-www-form-urlencoded")
$headers.Add("Accept", "application/json")


try
{
   $response = Invoke-RestMethod -Uri $tokenEndpoint -Method POST -Headers $headers -Body $body
   $response  #display results, if token is included should be able to parse out as variable
   $response | Export-Csv "C:\temp\access token.csv"
}
catch
{
   $ErrorMessage = $_.Exception.Message
   $b = $shell.popup($ErrorMessage,5,"ERROR",4096)
}



I would assume this could be re-written in WB code.  But like VBA or other code WB can integrate it is good to know that it works in the first place ;D