Nested App/Endpoint Authorization

I have a dashboard app that calls an API endpoint. The dashboard has the “grant app token” option enabled and so I am using -Headers @{ Authorization = “Bearer $PSUAppToken” } to pass the dashboard user’s credential to the endpoint. I’d like this endpoint to then call a second endpoint. Is there a way to pass the credential through again? I’m trying to add the same bearer token to the header but it doesn’t seem to be working.

Product: PowerShell Universal
Version: 4.5.4

Pass the token into the body and retrieve it into a variable in the first endpoint. You can the pass the variable again into the header of the second endpoint call.

Code to call first endpoint something like below

Invoke-restmethod -uri '/endpoint1' -headers @{ authorization = "bearer $PSUAppToken" } -body @{ psuapptoken = $PSUAppToken }

Then code in endpoint1’ something like below

$PSUAppToken = $Body.psuapptoken
Invoke-restmethod -uri '/endpoint2'  -headers @{ authorization = "bearer $PSUAppToken" }

I’ve also used another method where I store the token in cache and retrieve it from there.

Set the token in your dashboard

Set-psucache -key psuapptoken -value $PSUAppToken -persist

Retrieve it in your endpoint

$PSUAppToken = Get-psucache -key psuapptoken