1

I'm trying to get an oauth token from my Azure AD using Powershell.

I try to generate my token into a text file to see the result for the 1rst step of my request.

I'm trying to get an Azure Digital Twins token. The constant you see in $resourceId is given by Microsoft to request AzureDigitalTwins. And here is my script :

# Load ADAL methods
Add-Type -Path ".\MyPath\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"

$resultToken = ".\TokenTest.txt"

# Conf here
$aadInstance = "https://login.microsoftonline.com/"
$tenantId = "myTenantID"
$applicationId = "myAppID"
$applicationSecretKey = "myAppSecret"
$resourceId = "0b07f429-9f4b-4714-9392-cc5e8e80c8b0"


# Get an Access Token with ADAL
$authContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext($aadInstance + $tenantId)
$clientCredential = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential($applicationId, $applicationSecretKey)
$authenticationResult = $authContext.AcquireTokenAsync($resourceId, $clientCredential)
($token = $authenticationResult.AccessToken) | Out-File $resultToken 

After i run the script my Text file is empty but i get no error.

I use the exact same code in C# to get a token and it's working perfectly but not in Powershell apparently.

Is there a problem with this ? Thanks for your answers.

Xyluun
  • 288
  • 1
  • 2
  • 8

1 Answers1

2

Found the answer ! Just needed to add .GetAwaiter().GetResult() to the AcquireTokenAsync method !

$authenticationResult = $authContext.AcquireTokenAsync($resourceId, $clientCredential).GetAwaiter().GetResult()
Xyluun
  • 288
  • 1
  • 2
  • 8