As detailed in this blog by @vishnugillela, here is the one of the methods which we use to get access token from managed identity login with Azure AD for an Azure function app resource.
Instead of using Get-AzAccessToken
, I used the Invoke-WebRequest
PowerShell command to retrieve the function app resource access token.
I tried the below PS script in my environment by providing my function app resource URL as shown and it was successfully generated the access token.
$Uri = 'https://myfunctionapp.azurewebsites.net'
$res = Invoke-WebRequest -Uri $Uri -Method GET -Headers @{Metadata="true"} -UseBasicParsing
$content = $res.Content | ConvertFrom-Json
$generatedaccessToken = $content.access_token
$generatedaccessToken

Alternatively, you can use PowerShell functions to generate the access token for a particular azure resource as given in the above referenced article. I also tried the same in my environment by making few modifications and it worked as expected.
function Get-AccToken {
$con = Get-AzContext
$profile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($profile)
$tokenretrieve = $profileClient.AcquireAccessToken($con.Subscription.TenantId)
return $tokenretrieval.AccessToken
}
$subscriptionid = "subscriptionID"
$authHeader = @{
'Content-Type' = 'application/json'
'Authorization' = 'Bearer ' + (Get-AccToken)
}
$url = "https://myfunctionapp.azurewebsites.net"
$response = Invoke-RestMethod -Method Get -Headers $authHeader -Uri $url
$response
